Javascript &引用;无法调用方法';在';“未定义”的定义;问题
如何解决 查看:Javascript &引用;无法调用方法';在';“未定义”的定义;问题,javascript,backbone.js,Javascript,Backbone.js,如何解决 查看: 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
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
});
路由器:
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
});
收藏:
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
});
路由器将集合传递给视图构造函数
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);
}
...
});