While working with Backbone/Marionette, I came across something unusual. When I create a new instance of a view with a new collection property and then create another instance of the same view, it seems that the collection property of the second view points back to the first view's instance. Here's an example:
var CollectionView = Marionette.CollectionView.extend({
collection: new Backbone.Collection()
});
var view = new CollectionView();
console.log('view is ', view);
view.collection.searchTerm = 'test';
console.log('view.collection is ', view.collection);
this.region.show(view);
var view2 = new CollectionView();
console.log('view2 is ', view2);
console.log('view2.collection is ', view2.collection);
this.region.show(view2);
Upon inspecting the logs, you'll notice that there are two different view instances (one with cid: "view2" and the other with cid: "view5"). However, the collection property of the second view contains a property searchTerm
with a value of 'test'. One would expect this to be a completely new Backbone collection...
Feel free to check out the Codepen demo here.