Skip to content

Commit

Permalink
Add at end perf improvement can only occur if no sorting occurred
Browse files Browse the repository at this point in the history
Sorting requires reappending of all children.  Rather than handling all of the various flag states to determine this, we can simply remove the added views cache if a sort occurs.
  • Loading branch information
paulfalgout committed Nov 5, 2017
1 parent 3eaf749 commit cd8734c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 8 additions & 9 deletions src/next-collection-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ const CollectionView = Backbone.View.extend({

if (!viewComparator) { return; }

// If children are sorted prevent added to end perf
delete this._addedViews;

this.triggerMethod('before:sort', this);

this.children._sort(viewComparator, this);
Expand Down Expand Up @@ -427,21 +430,14 @@ const CollectionView = Backbone.View.extend({
return this;
},

_isAddedAtEnd(addedView, index, addedViews) {
const viewIndex = this.children._views.length - addedViews.length + index;
return addedView === this.children._views[viewIndex];
},

_filterChildren() {
const viewFilter = this._getFilter();
const addedViews = this._addedViews;

delete this._addedViews;

if (!viewFilter) {
if (!this.sortWithCollection && addedViews && _.every(addedViews, _.bind(this._isAddedAtEnd, this))) {
return addedViews;
}
if (addedViews) { return addedViews; }

return this.children._views;
}
Expand Down Expand Up @@ -621,8 +617,11 @@ const CollectionView = Backbone.View.extend({
return view;
}

// Only cache views if added to the end
if (!index || index >= this.children.length) {
this._addedViews = [view];
}
this._addChild(view, index);
this._addedViews = [view];
this._showChildren();

return view;
Expand Down
6 changes: 3 additions & 3 deletions test/unit/next-collection-view/collection-view-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('next CollectionView Data', function() {
let myCollectionView;
let collection;

describe('when sortWithCollection is true', function() {
describe('when children are sorted', function() {
beforeEach(function() {
collection = new Backbone.Collection([{ id: 1 }, { id: 2 }, { id: 3 }]);

Expand All @@ -233,11 +233,11 @@ describe('next CollectionView Data', function() {
});
});

describe('when sortWithCollection is false', function() {
describe('when children are not sorted', function() {
beforeEach(function() {
collection = new Backbone.Collection([{ id: 1 }, { id: 2 }, { id: 3 }]);

myCollectionView = new MyCollectionView({ collection, sortWithCollection: false });
myCollectionView = new MyCollectionView({ collection, viewComparator: false });
myCollectionView.render();
});

Expand Down

0 comments on commit cd8734c

Please sign in to comment.