出于某种原因,我的代码总是返回相同的模型,即使在我在 listView 页面中选择项目后我可以看到传入了正确的 id。
// main.js (relavent function)
venueDetails: function (id) {
// here, id is correct
var venue = new Venue({_id: id});
venue.fetch({success: function(){
// here, the id of venue is changed for some reason
console.log(venue.id);
$("#content").html(new VenueView({model: venue}).el);
}});
this.headerView.selectMenuItem();
},
该模型,
// model.js (relavent model)
var base = 'http://localhost:3000';
window.Venue = Backbone.Model.extend({
urlRoot: base+"/venues",
idAttribute: "_id",
});
window.VenueCollection = Backbone.Collection.extend({
model: Venue,
url: base+"/venues?populate=true"
});
在图片中,您可以看到 model.id 与 url 中的内容不同的地方
编辑:添加路由器映射
var AppRouter = Backbone.Router.extend({
routes: {
"" : "home",
<!-- venues -->
"venues" : "venueList",
"venues/page/:page" : "venueList",
"venues/add" : "addVenue",
"venues/:id" : "venueDetails",