2016-07-10 4 views
0

뷰에 전달 된 컬렉션을 참조하고 사용하는 방법을 이해하려고합니다. 컬렉션에 대한 참조가없는 것 같지만 모델이 사용되고 있습니다. 또한 내 API에 바인딩 된 컬렉션을 사용할 때 컬렉션 항목을 바인딩하거나 표시 할 수 없지만 하드 코딩 된 컬렉션을 사용할 때는 작동합니다. 어느 시점에서 내 컬렉션을 가져와야하나요? 제 컬렉션이 좋고 경로가 좋습니다. 나는 내 애플 리케이션 내내 아무런 문제없이 그것을 사용합니다. - (가) 컬렉션 통과 않습니다 참조받을백본 목록 컬렉션에 바인딩

module.exports = Backbone.Collection.extend({ 

    model: Employee, 

    url:"api/employees" 

}); 

MAINVIEW

module.exports = base.extend({ 
     el: '#content', 
     template:template, 
     initialize: function() { 

      // var items = new EmployeeCollection([ 
      //  {id: 1, firstName: "Test1 fName", lastName: "Test1 lName"}, 
      //  {id: 2, firstName: "Test2 fName", lastName: "Test2 lName"}, 
      //  {id: 3, firstName: "Test3 fName", lastName: "Test3 lName"} 
      // ]); 

      EmployeeListCollection = new EmployeeCollection(); 
      //this.itemView = new EmployeeListView({collection: items}); //this syntax works if I uncomment the code above to create my item list 
      this.itemView = new EmployeeListView({collection: EmployeeListCollection}); //do i need to fetch the collection at some point? 

      this.render(); 

      }, 
      render: function(){ 

      this.el.innerHTML = Mustache.to_html(this.template); 

      this.itemView.render(); 

      $("#empList").html(this.itemView.el); 
      } 
}); 

ItemListView : 아래

코드인가? 모델 참조가 보이지만 컬렉션을 전달했습니다.

답변

0

실제로 문제가 무엇인지 생각했습니다. 실제로 ItemListView에서 컬렉션을 가져와야했습니다. 렌더링이 렌더링 항목보다 먼저 호출되고 컬렉션의 각 모델이 renderitem 함수로 전달된다는 사실을 알게되었습니다. 내 렌더링에서 불러 오기를 호출하여 내 컬렉션을 가져올 수있었습니다.

var self = this; 
this.collection.fetch().done(function(){ 
    self.collection.each(self.renderItem); 
}); 

이제는 모두 의미가 있습니다. 하지만 여전히 내 코드가 두 번 실행되는 이유를 완전히 이해하지 못합니다. MainView의 초기화 및 렌더링에서 console.log를 수행 할 때마다 처음 실행할 때마다 두 번 호출됩니다.

+0

자신의 대답조차도 [수용 가능] (http://stackoverflow.com/help/accepted-answer)이 아닙니다. – pnuts

관련 문제