2013-05-15 3 views
2

백본 컬렉션으로 밑줄을 사용하는 데 어려움을 겪었습니다.백본이 "this"로 작동하지 않습니까?

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을 사용으로?

얻을 경우 인스턴스에 완벽하게 작동합니다.

var coll=new collection; 

coll.get(id); //working fine 
+0

음을 어떻게되는지, 그 이상하다, 나는 빠른 jsfiddle을 만들어 그것을 작동 : [바이올린] (http://jsfiddle.net/y7gDN/) (출력을위한 콘솔을 확인). – Ingro

+0

'coll.getModelEntry (id)'는'undefined'를 리턴합니까? 아주 이상한 .. –

+0

백본 모델의 idAttribute가 id가 아닌 다른 것으로 설정되지 않았습니까? – jbl

답변

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'));     
      }   
     }  
관련 문제