Backbone得不到“这个”?

时间:2013-05-15 07:18:20

标签: javascript jquery html html5 backbone.js

我只是努力使用下划线获取主干集合。

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 in collection有什么不对?

get在实例上工作得很完美!

var coll=new collection;

coll.get(id); //working fine

1 个答案:

答案 0 :(得分: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'));                
            }        
        }       
相关问题