admin管理员组文章数量:1400201
I'm having some error in my app and don't know why.
Uncaught TypeError: Cannot call method 'on' of undefined
It happens on my collection view, follow the code:
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);
}
});
the problem is on:
this.collection.on('add', this.addOne, this);
Anyone knows why?
** Edit: Collection code **
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: my Router code where i'm instantiating 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();
I'm having some error in my app and don't know why.
Uncaught TypeError: Cannot call method 'on' of undefined
It happens on my collection view, follow the code:
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);
}
});
the problem is on:
this.collection.on('add', this.addOne, this);
Anyone knows why?
** Edit: Collection code **
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: my Router code where i'm instantiating 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();
Share
Improve this question
edited May 31, 2013 at 15:35
Guilherme
asked May 31, 2013 at 15:24
GuilhermeGuilherme
1,1031 gold badge15 silver badges29 bronze badges
3
-
What exactly is
this.collection
? – Danilo Valente Commented May 31, 2013 at 15:26 - 1 Can you show the code you're using to instantiate the view? – net.uk.sweet Commented May 31, 2013 at 15:28
- @MarkLinus Workout collection, i will add the code – Guilherme Commented May 31, 2013 at 15:37
1 Answer
Reset to default 6 this.workoutsView = new App.WorkoutsView();
is supposed to pass in a collection
this.workoutsView = new App.WorkoutsView({collection : this.workoutItems});
Because you view when being initialized is expecting to have a collection available to it as you are binding a event to it in the Initialize method of the view.
本文标签: javascriptUncaught TypeError Cannot call method 39on39 of undefinedBackbonejsStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Cannot call method 'on' of undefined - Backbone.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744189884a2594469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论