change collection change the collection in other views
by seddass
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
JavaScript
var Collection = Backbone.Collection.extend({});
var coll = new Collection();
coll.add({
name: 'model name',
domain: 'model domain'
});
var MyView1 = Backbone.View.extend({
collection: coll,
initialize: function () {
this.render();
},
render: function () {
console.log('view1', this.collection.length);
this.collection.add({
name: 'model name',
domain: 'model domain'
});
}
});
var MyView2 = Backbone.View.extend({
collection: coll,
initialize: function() {
this.collection.bind('add', this.render, this);
},
render: function() {
console.log('view2', this.collection.length);
}
});
var myView2 = new MyView2();
var myView1 = new MyView1();