[英]Backbone.js - Uncaught TypeError: Cannot call method 'receivedEvent' of undefined
[英]Uncaught TypeError: Cannot call method 'on' of undefined - 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.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);
}
});
編輯:我的路由器代碼,我在實例化WorkoutsView:
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();
應該傳遞一個集合
this.workoutsView = new App.WorkoutsView({collection : this.workoutItems});
因為您在初始化時查看期望有一個集合可用,因為您在視圖的Initialize方法中將事件綁定到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.