【发布时间】:2014-07-09 20:29:44
【问题描述】:
我有一个主干JS 应用程序,它有一个看起来像的路由器
var StoreRouter = Backbone.Router.extend({
routes: {
'stores/add/' : 'add',
'stores/edit/:id': 'edit'
},
add: function(){
var addStoresView = new AddStoresView({
el: ".wrapper"
});
},
edit: function(id){
var editStoresView = new EditStoresView({
el: ".wrapper",
model: new Store({ id: id })
});
}
});
var storeRouter = new StoreRouter();
Backbone.history.start({ pushState: true, hashChange: false });
还有一个看起来像这样的模型:
var Store = Backbone.Model.extend({
urlRoot: "/stores/"
});
然后我的视图看起来像:
var EditStoresView = Backbone.View.extend({
...
render: function() {
this.model.fetch({
success : function(model, response, options) {
this.$el.append ( JST['tmpl/' + "edit"] (model.toJSON()) );
}
});
}
我认为获取 urlRoot 时会调用 /stores/ID_HERE,但现在它不调用它,它只是调用 /stores/,但我我不知道为什么以及如何解决这个问题?
在 devTools 中,这是它要访问的 url:
GET http://localhost/stores/
【问题讨论】:
-
您需要发布更多的来源,设置
urlRoot可能不是问题。 -
@Whymarrh,当然,哪些部分会有所帮助?剩下的视图代码?
-
@the_:问题不在于您在此处提供的代码 - 我尝试完成它并且正确生成了 URL。随意将
console.log(id)添加到StoreRouter.edit(),也许id参数设置不正确。或者,您的应用程序中可能还有其他代码路径,而不是您在此处复制的路径。 -
@WladimirPalant,好的,我做了 console.log(id);在 StoreRouter.edit() 中,但它只是正确地返回了 id?
-
@WladimirPalant,我刚刚编辑并添加了我唯一的其他路线功能。
标签: javascript jquery backbone.js model underscore.js