Javascript 未捕获类型错误:无法调用方法';在';未定义的类型-Backbone.js
我的应用程序有一些错误,不知道为什么Javascript 未捕获类型错误:无法调用方法';在';未定义的类型-Backbone.js,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我的应用程序有一些错误,不知道为什么 Uncaught TypeError: Cannot call method 'on' of undefined 它发生在“我的收藏”视图中,请遵循以下代码: App.WorkoutsView = Backbone.View.extend({ initialize: function(){ this.collection.on('add', this.addOne, this); this.collection.o
Uncaught TypeError: Cannot call method 'on' of undefined
它发生在“我的收藏”视图中,请遵循以下代码:
App.WorkoutsView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.addAll, this);
},
render: function() {
this.addAll();
return this;
},
addAll: function() {
this.$el.empty();
this.collection.forEach(this.addOne, this);
},
addOne: function(Workout) {
var workoutView = new App.WorkoutView({model: App.Workout});
this.$el.append(workoutView.render().el);
}
});
问题在于:
this.collection.on('add', this.addOne, this);
有人知道为什么吗
**编辑:收集代码**
App.WorkoutItems = Backbone.Collection.extend({
model: App.Workout,
url: '/workouts',
localStorage: function() {new Backbone.LocalStorage('Workout')},
initialize: function() {
this.on('remove', this.hideWorkout, this);
},
hideWorkout: function() {
model.trigger('hide');
},
focusOnWorkoutItem: function() {
var modelsToRemove = this.filter(function(workoutItem) {
return workoutItem.id != id;
});
this.remove(modelsToRemove);
}
});
编辑:我正在实例化工作的路由器代码视图:
App.Router = Backbone.Router.extend({
routes: {
'':'index'
},
initialize: function() {
this.workoutItems = new App.WorkoutItems();
this.workoutsView = new App.WorkoutsView();
this.workoutsView.render();
},
index: function() {
$('#workouts').html(this.workoutsView.el);
this.workoutsItem.fetch();
}
});
new App.Router;
Backbone.history.start();
应该传入一个集合
this.workoutsView = new App.WorkoutsView({collection : this.workoutItems});
因为在初始化视图时,当您在视图的Initialize方法中将事件绑定到视图时,您希望视图有一个可用的集合。此集合的确切含义是什么。?能否显示用于实例化视图的代码?@MarkLinus collection,我将添加代码
this.workoutsView = new App.WorkoutsView({collection : this.workoutItems});