如何解决?
查看:
App.Views.ModelsIndex = Backbone.View.extend({
tagName: 'div',
initialize: function() {
this.template = _.template($('#container').html());
// Cannot call method 'on' of undefined
this.model.on('change', this.render, this);
},
render: function() {
$(this.el).html(this.template({'model' : this.model}));
return this;
}
});
路由器:
App.Routers.Models = Backbone.Router.extend({
routes: {
'': 'index',
'admin/:id' : 'show'
},
initialize: function() {
this.collection = new App.Collections.Models();
this.collection.fetch();
},
index: function() {
view = new App.Views.ModelsIndex({'collection' : this.collection});
$('#container').html(view.render().el);
},
show: function(id) {
alert('entry id:' + id);
}
});
收集:
App.Collections.Models = Backbone.Collection.extend({
url: "/app_dev.php/partner/api/users/1"
, model: App.Models.Model
});
答案 0 :(得分:2)
您的路由器将集合传递给您的视图构造函数
new App.Views.ModelsIndex({'collection' : this.collection});
但您在视图中使用了模型:
this.model.on('change', this.render, this);
选择一个并坚持下去:
App.Views.ModelsIndex = Backbone.View.extend({
initialize: function() {
this.template = _.template($('#container').html());
this.collection.on('change', this.render, this);
}
...
});