我试图为通过链接访问的《邮报》设置一个显示页:
<a class="post-link button" href="#">details</a>
当邮寄项目完成时, 给它一个像这样的href :
this.$( a ).attr( href , this.postUrl());
...
postUrl: function() {
return "#posts/" + this.model.get( id );
}
当字段完成时,链接显示为:
http://[path_to_app]/#posts/[whatever_id]
当我点击链接时, 它的 应该 击中这个路由器 :
routes: {
: index ,
new : newPost ,
"posts/:id" : show
}
但我得到这个错误, 它从来没有进入到显示函数 在所有的:
Uncaught Error: Syntax error, unrecognized expression: /1 jquery.js:4268
Sizzle.errorjquery.js:4268
Sizzle.filterjquery.js:4254
Sizzlejquery.js:4044
Sizzlejquery.js:5176
jQuery.fn.extend.findjquery.js:5432
jQuery.fn.jQuery.initjquery.js:188
jQueryjquery.js:28
hashchangetabs.js:9
jQuery.event.dispatchjquery.js:3333
jQuery.event.add.elemData.handle.eventHandle
当我将链接改为“#post/” + id ” 和“post/: id” 路径时,这个错误就不会发生。
我正在学习 Backbone, 并在此真的需要一只手。 还有一点值得注意:我的路径根到#index 行动 。
EDIT(更相关的代码):
# Model
[APP].Models.Post = Backbone.Model.extend({
urlRoot: /posts
});
#Router
[APP].Routers.Posts = Support.SwappingRouter.extend({
initialize: function(options) {
this.el = $( #posts );
this.collection = options.collection;
},
routes: {
: index ,
new : newPost ,
"posts/:id" : show
},
index: function() {
var view = new [APP].Views.PostsIndex({ collection: this.collection });
this.swap(view);
},
newPost: function() {
var view = new [APP].Views.PostsNew({ collection: this.collection });
this.swap(view);
},
show: function(postId) {
var post = this.collection.get(postId);
var postsRouter = this;
post.fetch({
success: function() {
var view = new [APP].Views.PostShow({ model: post });
postsRouter.swap(view);
}
});
}
});
#Post Item View
[APP].Views.PostItem = Backbone.View.extend({
tagName: tr ,
initialize: function() {
_.bindAll(this, render );
},
render: function () {
this.$el.attr( id , post_ + this.model.id);
this.$el.html(JST[ posts/item ]({ post: this.model }));
this.renderFormContents();
return this;
},
renderFormContents: function() {
...
this.$( a ).attr( href , this.postUrl());
},
postUrl: function() {
return "#posts/" + this.model.get( id );
}
});