Ho qualche errore nella mia app e non so perché.Unchaught TypeError: impossibile richiamare il metodo 'on' di undefined - Backbone.js
Uncaught TypeError: Cannot call method 'on' of undefined
Succede sul mio punto di vista collezione, seguire il codice:
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);
}
});
il problema è su:
this.collection.on('add', this.addOne, this);
Chiunque sa perché?
** Edit: Codice collezione **
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);
}
});
Edit: il mio codice Router dove sto istanziare 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();
Che cosa è esattamente 'this.collection'? –
Puoi mostrare il codice che stai utilizzando per istanziare la vista? –
@MarkLinus Workout collection, aggiungerò il codice –