2

我只是努力将下划线 get 与骨干集合一起使用。

var collection=Backbone.Collection.extend({
model:someModel,
getModelEntry : function(id){
 return this.get(id);

//returns undefined
}
})

尝试2:

var collection=Backbone.Collection.extend({
     model:someModel,
    getModelEntry : function(id){
     var model = this.where({id:id})[0];
    //here I got model
     return model.get("attr");
    //returns undefined
    }
    });

在集合中使用 get 有什么问题?

get 在实例上运行完美!

var coll=new collection;

coll.get(id); //working fine
4

1 回答 1

0

据我所见,工作正常。检查您要查找的模型的 id 是否存在于您的集合中。添加类似下面的内容,看看会发生什么

    getModelEntry : function(id){            
            var model = this.get(id);
            if(model == undefined) {
                console.log("id: ",id);
                console.log("collection: ",JSON.stringify(this.models));
            } else {
               console.log(model.get('name'));                
            }        
        }       
于 2013-05-15T08:03:00.793 回答