diff --git a/spec/javascripts/collectionView.spec.js b/spec/javascripts/collectionView.spec.js index 8524282c27..05320e35af 100644 --- a/spec/javascripts/collectionView.spec.js +++ b/spec/javascripts/collectionView.spec.js @@ -68,6 +68,7 @@ describe('collection view', function() { this.sinon.spy(this.collectionView.$el, 'append'); this.sinon.spy(this.collectionView, 'startBuffering'); this.sinon.spy(this.collectionView, 'endBuffering'); + this.sinon.spy(this.collectionView, 'getChildView'); this.collectionView.render(); }); @@ -153,6 +154,12 @@ describe('collection view', function() { it('should trigger "childview:render" for each item in the collection', function() { expect(this.childViewRender.callCount).to.equal(2); }); + + it('should call "getChildView" for each item in the collection', function() { + expect(this.collectionView.getChildView).to.have.been.calledTwice. + and.calledWith(this.collection.models[0]). + and.calledWith(this.collection.models[1]); + }); }); describe('when rendering a collection view and accessing children via the DOM', function() { @@ -783,6 +790,7 @@ describe('collection view', function() { this.collectionView.trigger('show'); this.sinon.spy(this.collectionView, 'attachBuffer'); + this.sinon.spy(this.collectionView, 'getChildView'); this.collection.add(this.model2); this.view = this.collectionView.children.findByIndex(1); @@ -803,6 +811,10 @@ describe('collection view', function() { it('should call the childs "onDomRefresh" method with itself as the context', function() { expect(this.ChildView.prototype.onDomRefresh).to.have.been.called; }); + + it('should call "getChildView" with the new model', function() { + expect(this.collectionView.getChildView).to.have.been.calledWith(this.model2); + }); }); describe('when setting an childView in the constructor options', function() { diff --git a/src/marionette.collectionview.js b/src/marionette.collectionview.js index d90f1b9819..e078e34e28 100644 --- a/src/marionette.collectionview.js +++ b/src/marionette.collectionview.js @@ -226,9 +226,12 @@ Marionette.CollectionView = Marionette.View.extend({ } }, - // Retrieve the childView class, either from `this.options.childView` + // Retrieve the `childView` class, either from `this.options.childView` // or from the `childView` in the object definition. The "options" // takes precedence. + // This method receives the model that will be passed to the instance + // created from this `childView`. Overriding methods may use the child + // to determine what `childView` class to return. getChildView: function(child) { var childView = this.getOption('childView');