2013-03-27 2 views
0

서로 다른 상황에서 두 개의보기를 만들고 컬렉션을 변경하려고했습니다. 나는 모든 컬렉션 변경시 이벤트 렌더링을 발생 시키도록 this.collection.bind를 설정하는 방법을 모른다.백본이 렌더링 이벤트를 실행하지 않음

BusinessListView 를 볼 수있는 내가 render

다른보기이다 this.collection = new Businesses([{ name: '3'}, { name: '4' }]);
  • this.search_location = new SearchLocation();를 호출
    1. this.businesslist.collection = new Businesses([{ name: '1'}, { name: '2' }]);
    2. this.businesslist.set();은, 다음 수집을 보내 해고보기 BusinessListView을 기대할 3 상황이 있습니다

      나는 기대하고 있었다. 1과 2의 콘솔에서 데이터가 작동하지 않습니다. .render()를 수동으로 추가하면 컬렉션이 변경된 것을 볼 수 있습니다. 어떻게 작동하는지 설명해 주시겠습니까?

      UPDATE이 완벽하게 작동 솔루션 알렉스에

      감사합니다 : 당신이 다른 인스턴스에 this.collection 기준 설정 계속

      http://jsfiddle.net/feronovak/RAPjM/

      var App = { 
          run: function() { 
           this.businesslist = new BusinessListView(); 
           this.businesslist.collection = new Businesses([{ name: '1'}, { name: '2' }]); 
           // this.businesslist.render(); // uncomment to see collection change 
           this.businesslist.set(); 
      
           this.search_location = new SearchLocation(); 
          } 
      }; 
      
      Business = Backbone.Model.extend({}); 
      Businesses = Backbone.Collection.extend({ 
          model: Business 
      }); 
      
      BusinessListView = Backbone.View.extend({ 
          initialize: function(options) { 
           this.collection = new Businesses(); 
           this.collection.bind("reset", this.render(), this); 
          }, 
          render: function() { 
           console.log(this.collection.toJSON()); 
          }, 
          set: function() 
          { 
           this.collection = new Businesses([{ name: '3'}, { name: '4' }]); 
           // this.render(); // uncomment to see collection change 
          } 
      }); 
      
      SearchLocation = Backbone.View.extend({ 
          el: "#search", 
          initialize: function() { 
           this.sendData(); 
          }, 
          sendData: function() { 
           // Send [{ name: '5'}, { name: '6' }] to this.businesslist = new Businesses([{ name: '5'}, { name: '6' }]); 
          } 
      }); 
      
      $(document).ready(function(e) { 
          App.run(); 
      }); 
      
  • +0

    모음집 전화 리셋 이벤트를 변경하지 않습니다? 컬렉션을 수동으로 변경하면 (이 예제에서만) 렌더가 호출되기를 기대했습니다. – feronovak

    +0

    내가 잘못 추측 한대로 내 마지막 코멘트를 삭제했습니다. 귀하의 jsfiddle 렌더링 메서드가 실행되는 것을 보여줍니다. – Loamhoof

    +0

    새 오브젝트를 작성하면 리스너가 사라집니다. 그래서 당신은 새로운 것을 듣지 않고 있습니다. – Loamhoof

    답변

    1

    은. 그것은 "초기화"에서 참조 된 객체를 실제로 재설정하지 않기 때문에 "재설정"되지 않습니다. 대신

    :

    set: function() 
        { 
         this.collection = new Businesses([{ name: '3'}, { name: '4' }]); 
        } 
    

    시도 :

    set: function() 
        { 
         this.collection.reset([{ name: '3'}, { name: '4' }]); 
        } 
    

    및 실행에서 제거 : 여기

    this.businesslist.collection = new Businesses([{ name: '1'}, { name: '2' }]); 
    

    예 : http://jsfiddle.net/aXJ9x/1/

    +0

    this.render()도 트리거하지 않았습니다. 이 줄 (this.businesslist.collection)은 거기에 목적에 따라 다른 방법을 사용하여 컬렉션을 수정하는 방법을 보여줍니다. jsfiddle를 포크로 작업 예제를 표시 할 수 있습니까? – feronovak

    +0

    피들 포스트의 하단에 추가 –

    +0

    감사합니다, 좋은 작품. SearchLocation보기에서 콜렉션 5,6을 호출하려면 어떻게해야합니까? – feronovak

    관련 문제