我的模型和集合看起来像这样.
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...
});
}
});
Run Code Online (Sandbox Code Playgroud)
当调用item.foo()时,浏览器抛出错误:Uncaught TypeError:Object [object Object]没有方法'foo'任何人都可以帮我弄清楚这里出了什么问题.