My model and collection looks like this.
var MyModel= Backbone.Model.Extend({
foo: function(){
alert("is not working...");
}
});
var MyCol = Backbone.Collection.extend({
models:MyModel,
url: function(){ return '/json_from_server' }, //json data mapped to MyModel
poo:function(){
alert("this works");
}
});
cols = new MyCol();
cols.fetch({
success:function () {
cols.poo(); //this works fine
cols.models.forEach(function(item){
alert(item.get("id")); //It works fine
alert(item.foo()); // this is not working...
});
}
});
when invoking item.foo(), the browser throws the error: Uncaught TypeError: Object [object Object] has no method ‘foo’
Can anybody help me to figure out what’s going wrong here.
In your
MyColobject, you setmodels: MyModel. I believe models should not be plural, so it should bemodel: MyModel.If that still doesn’t work, you might need a _.bindAll in your MyModel like so:
The reason your initialize didn’t work earlier was because as stated above, you had “models” as plural when it should just be
model.