2013-07-07 5 views
0

컬렉션 뷰를 모델 뷰에 중첩하려고합니다.중첩 컬렉션을 백본으로 초기화

MyApp.addInitializer(function(options){ 
    var heroes = new Heroes(options.heroes); 

    // each hero's villains must be a backbone collection 
    // we initialize them here 
    heroes.each(function(hero){ 
    var villains = hero.get('villains'); 
    var villainCollection = new Villains(villains); 
    hero.set('villains', villainCollection); 
    }); 

    // edited for brevity 

}); 

어떻게없이 같은 일을 갈 것입니다 :

는이를 위해, 나는 그가이 같은 중첩 된 콜렉션 뷰를 초기화 끝에서 백본의 Marionnette 복합보기 및 followed that tutorial

을 사용 Marionette에서 addInitalizer를 사용 하시겠습니까?

내 프로젝트에서 서버의 데이터를 필터링하고 있습니다. 내가 좋아하는 일을하려고했을 때 :

App.candidatures = new App.Collections.Candidatures; 

App.candidatures.fetch({reset: true}).done(function() { 
    App.candidatures.each(function(candidature) { 
     var contacts = candidature.get('contacts'); 
     var contactCollection = new App.Collections.Contacts(contacts); 
     candidature.set('contacts', contactCollection); 
    }); 
    new App.Views.App({collection: App.candidatures}); 


}); 

내가 얻을 컬렉션에서 오는 "indefined 옵션"

App.Collections.Contacts = Backbone.Collection.extend({ 
model: App.Models.Contact, 

initialize:function(models, options) { 
    this.candidature = options.candidature; 
}, 


url:function() { 
    return this.candidature.url() + "/contacts"; 
} 
)}; 

답변

0

당신이 contactCollection를 만들 때, 당신이 제공하지 않는 때문이다 options 개체의 candidatures 컬렉션 당신은 같은에 연락처 수집 초기화 코드를 수정해야합니까 :

initialize:function(models, options) { 
    this.candidature = options && options.candidature; 
} 

candidature 속성이 제공된 값으로 설정됩니다 그런 식으로 (제공되지 않음 경우, 그것은 undefined 될 것입니다).

App.candidatures.each(function(candidature) { 
    var contacts = candidature.get('contacts'); 
    var contactCollection = new App.Collections.Contacts(contacts, { 
     candidature: candidature 
    }); 
    candidature.set('contacts', contactCollection); 
}); 

P.S : 난 당신이 내 블로그 게시물이 도움이 되었기를 바랍니다 :

그렇다면, 당신은 아직도 당신이 컬렉션을 instanciating있는 정보를 제공해야합니다!

+0

안녕하세요 David! 여기에서 대답을 얻으 려합니다. 귀하의 게시물 정말 멋지 네요! 나는 candidature.contacts가 이미 설정되어 있음을 알고 있지만 데이터를 검색 할 수는 없습니다. (귀하의 제안에 따라 수정도 가능합니다.) Contact Model – user2167526

+0

죄송합니다. 코드를 약간 잘못 읽었습니다. 내 대답이 업데이트되었습니다. –

+0

감사합니다. 답장! 나는 점점 가까워지고 있다고 생각한다. (이것에 대한 1 주일 후에!).하지만 지금은 입후보자보기 (첫 번째 레벨)에서 console.log (this.model.get ('contacts')); 빈 개체 ... 아마도 내가 누락 된 일부 비동기적인 것일 수도 있습니다. ( – user2167526

관련 문제