I have a backbone model which contains an array which I am updating with a simple id (I didn’t want to add nested collection).
It seems fairly straightforward to me, but for some reason I’m getting an Uncaught TypeError: Object [object Object] has no method 'preshow' error.
I have a list of classes, and a form with the class being entered.
The user can click an item on the classlist to add it as a prerequisite to the class being edited. This triggers the ‘add_prerequisite’ in the ‘ClassInput’ view.
I’ve left out the code that I think is probably not needed.
Myapp.ClassInput = Backbone.Views.extend({
initialize: function(){
Myapp.class = this.model;
Myapp.class.bind("add_prerequisite", this.add_prerequisite);
},
add_prerequisite: function(prerequisite){
// this is a method in my model which adds the prerequisite id to this class
class.add_prerequisite(prerequisite)
//this is the line triggering the error
this.show_pre(prerequisite);
},
show_pre: function(prerequisite){
alert(prerequisite);
}
});
Myapp.Classes.ClassList = Backbone.Views.extend({
add_prerequisite: function(){
Myapp.class.trigger('add_prerequisite',this.model.id);
}
});
You need to bind the context of
add_prerequisiteto your backbone model.To do that, you just have to add a third argument to the bind method