2012-05-15 7 views
0

컬렉션에 뷰 바인딩을 정의했습니다.컬렉션을 가져올 때 렌더 함수가 호출되지 않음

1)보기를 초기화 할 때 콜렉션에서 가져 오기 호출이 수행됩니다.
2) get 요청 결과를 보면 괜찮습니다.
3) 렌더링 기능을 호출하기 위해 myCollection에서이 변경을 트리거하고 싶지만 작동하지 않습니다. 여기

내 코드의 일부입니다 : 내가 사용하는 경우

define([ 
    "MyCollection", 
    "!text/template" 
], function (myCollection, myTemplate) { 

    var myView = Backbone.View.extend({ 

     initialize: function() 
     { 
      myCollection.bind('change', this.render); // why the render is not called when 
                 // I perform the fetch even if the data is correctly loaded? 

      myCollection.fetch(); // it works 
     }, 

     render: function() 
     { 
      // some code 
      this.$el <-- undefined 
     } 

    }); 

    return myView; 
}); 

1)

initialize: function() 
    { 
     myCollection.bind('reset', this.render); // this.$el <-- undefined in render call, Why? 

     myCollection.fetch(); // it works 
    } 

2)

myCollection.fetch({ 
    success: function() { 
     that.render(); // <-- here the $el is defined and it works, Why myCollection.bind('reset', this.render); not?? 
     // it seems to me that when I use reset the render function is called before the this.$el is defined 
    } 
}); 

답변

1

change 이벤트가 아닌 reset 이벤트를 청취해야합니다.

그래서 : 내가 재설정에 변경하는 경우

myCollection.bind('reset', this.render, this); 
+0

의 성공적인 완료에 변경 이벤트 트리거 나는 다음과 같은 메시지가 얻을 :'catch되지 않은 형식 오류를 : 정의되지 않은 메소드 'html'을 호출 할 수 없습니다. 내 질문에 대한 자세한 내용 – underscore666

1

백본은 "reset" event 아닌 fetch()

var myView = Backbone.View.extend({ 

    initialize: function() 
    { 
     myCollection.bind('reset', this.render); 
     myCollection.fetch(); // it works 
    }, 

    render: function() 
    { 
     // some code 
    } 

}); 
+0

재설정시 변경되면 다음 메시지가 표시됩니다. Uncaught TypeError : 'html'undefined' 메서드를 호출 할 수 없습니다. 내 질문에 대한 자세한 내용 – underscore666

+0

엘을 어디에서 설정하고 있습니까? 인스턴스 생성시에? –

관련 문제