Skip to content

Commit

Permalink
Merge pull request #763 from lukaszfiszer/fix-composite-view-listeners
Browse files Browse the repository at this point in the history
Fix CompositeView adding child views before render
  • Loading branch information
samccone committed Nov 14, 2013
2 parents 374fe62 + a38e969 commit cf0491e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
52 changes: 52 additions & 0 deletions spec/javascripts/compositeView-itemViewContainer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,56 @@ describe("composite view - itemViewContainer", function(){
});
});


describe("when a composite view is not yet rendered", function(){
var CompositeView = Backbone.Marionette.CompositeView.extend({
itemView: ItemView,
itemViewContainer: "ul",
template: "#composite-child-container-template"
});

var compositeView, collection, model1, model2;

var addModel = function() {
collection.add([model2]);
};

var removeModel = function() {
collection.remove([model1]);
};

var resetCollection = function() {
collection.reset([model1, model2]);
};

beforeEach(function() {
loadFixtures("compositeChildContainerTemplate.html");
model1 = new Model({foo: "bar"});
model2 = new Model({foo: "baz"});
collection = new Collection([model1]);
compositeView = new CompositeView({
collection: collection
});
spyOn(compositeView, "addChildView").andCallThrough();
});

it('should not raise any errors when item is added to collection', function() {
expect(addModel).not.toThrow();
});

it('should not call addChildView when item is added to collection', function() {
addModel();
expect(compositeView.addChildView).not.toHaveBeenCalled();
});

it('should not raise any errors when item is removed from collection', function() {
expect(removeModel).not.toThrow();
});

it('should not raise any errors when collection is reset', function() {
expect(resetCollection).not.toThrow();
});

});

});
16 changes: 11 additions & 5 deletions src/marionette.compositeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ Marionette.CompositeView = Marionette.CollectionView.extend({
// binds to. Override this method to prevent the initial
// events, or to add your own initial events.
_initialEvents: function(){
if (this.collection){
this.listenTo(this.collection, "add", this.addChildView, this);
this.listenTo(this.collection, "remove", this.removeItemView, this);
this.listenTo(this.collection, "reset", this._renderChildren, this);
}

// Bind only after composite view in rendered to avoid adding child views
// to unexisting itemViewContainer
this.once('render', function () {
if (this.collection){
this.listenTo(this.collection, "add", this.addChildView, this);
this.listenTo(this.collection, "remove", this.removeItemView, this);
this.listenTo(this.collection, "reset", this._renderChildren, this);
}
});

},

// Retrieve the `itemView` to be used when rendering each of
Expand Down

0 comments on commit cf0491e

Please sign in to comment.