서로 다른 상황에서 두 개의보기를 만들고 컬렉션을 변경하려고했습니다. 나는 모든 컬렉션 변경시 이벤트 렌더링을 발생 시키도록 this.collection.bind를 설정하는 방법을 모른다.백본이 렌더링 이벤트를 실행하지 않음
BusinessListView
를 볼 수있는 내가 render
this.collection = new Businesses([{ name: '3'}, { name: '4' }]);
this.search_location = new SearchLocation();
를 호출
this.businesslist.collection = new Businesses([{ name: '1'}, { name: '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(); });
모음집 전화 리셋 이벤트를 변경하지 않습니다? 컬렉션을 수동으로 변경하면 (이 예제에서만) 렌더가 호출되기를 기대했습니다. – feronovak
내가 잘못 추측 한대로 내 마지막 코멘트를 삭제했습니다. 귀하의 jsfiddle 렌더링 메서드가 실행되는 것을 보여줍니다. – Loamhoof
새 오브젝트를 작성하면 리스너가 사라집니다. 그래서 당신은 새로운 것을 듣지 않고 있습니다. – Loamhoof