Skip to content

Commit

Permalink
Performant NextCollectionView destroy
Browse files Browse the repository at this point in the history
In cases where view events are not monitored, the collectionview can optimistically detach all of the children immediately prior to the `_.each` which is performance improvement.
  • Loading branch information
paulfalgout committed Aug 2, 2017
1 parent 4148f1f commit 39db0a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/next-collection-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ const CollectionView = Backbone.View.extend({
}

this.triggerMethod('before:destroy:children', this);
if (this.monitorViewEvents === false) {
this.Dom.detachContents();
}
_.each(this.children._views, _.bind(this._removeChildView, this));
this.triggerMethod('destroy:children', this);
}
Expand Down
15 changes: 14 additions & 1 deletion test/unit/next-collection-view/collection-view-children.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,24 +645,37 @@ describe('next CollectionView Children', function() {
myCollectionView.onDestroyChildren = this.sinon.stub();

myCollectionView.render();
myCollectionView.destroy();
});

it('should destroy each view', function() {
myCollectionView.destroy();
myCollectionView.children.each(view => {
expect(view.isDestroyed()).to.be.true;
});
});

it('should trigger "before:destroy:children"', function() {
myCollectionView.destroy();
expect(myCollectionView.onBeforeDestroyChildren)
.to.be.calledOnce.and.calledWith(myCollectionView);
});

it('should trigger "destroy:children"', function() {
myCollectionView.destroy();
expect(myCollectionView.onDestroyChildren)
.to.be.calledOnce.and.calledWith(myCollectionView);
});

describe('when view events are not monitored', function() {
it('should detach the contents from the dom prior to destroying', function() {
this.sinon.spy(myCollectionView.Dom, 'detachContents');
myCollectionView.monitorViewEvents = false;
myCollectionView.destroy();
expect(myCollectionView.Dom.detachContents).to.have.been.calledOnce
.and.calledAfter(myCollectionView.onBeforeDestroyChildren)
.and.calledBefore(myCollectionView.onDestroyChildren);
});
});
});

describe('when destroying the collectionView without children', function() {
Expand Down

0 comments on commit 39db0a8

Please sign in to comment.