Skip to content

Commit

Permalink
Merge pull request #1619 from cmaher/fix-1566
Browse files Browse the repository at this point in the history
Fix #1567 and add tests to cover getChildView taking a model
  • Loading branch information
samccone committed Jul 14, 2014
2 parents 9abb5eb + cfd85ad commit 719b300
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/javascripts/collectionView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion src/marionette.collectionview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 719b300

Please sign in to comment.