Skip to content

Commit cd8734c

Browse files
committed
Add at end perf improvement can only occur if no sorting occurred
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.
1 parent 3eaf749 commit cd8734c

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/next-collection-view.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ const CollectionView = Backbone.View.extend({
368368

369369
if (!viewComparator) { return; }
370370

371+
// If children are sorted prevent added to end perf
372+
delete this._addedViews;
373+
371374
this.triggerMethod('before:sort', this);
372375

373376
this.children._sort(viewComparator, this);
@@ -427,21 +430,14 @@ const CollectionView = Backbone.View.extend({
427430
return this;
428431
},
429432

430-
_isAddedAtEnd(addedView, index, addedViews) {
431-
const viewIndex = this.children._views.length - addedViews.length + index;
432-
return addedView === this.children._views[viewIndex];
433-
},
434-
435433
_filterChildren() {
436434
const viewFilter = this._getFilter();
437435
const addedViews = this._addedViews;
438436

439437
delete this._addedViews;
440438

441439
if (!viewFilter) {
442-
if (!this.sortWithCollection && addedViews && _.every(addedViews, _.bind(this._isAddedAtEnd, this))) {
443-
return addedViews;
444-
}
440+
if (addedViews) { return addedViews; }
445441

446442
return this.children._views;
447443
}
@@ -621,8 +617,11 @@ const CollectionView = Backbone.View.extend({
621617
return view;
622618
}
623619

620+
// Only cache views if added to the end
621+
if (!index || index >= this.children.length) {
622+
this._addedViews = [view];
623+
}
624624
this._addChild(view, index);
625-
this._addedViews = [view];
626625
this._showChildren();
627626

628627
return view;

test/unit/next-collection-view/collection-view-data.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ describe('next CollectionView Data', function() {
209209
let myCollectionView;
210210
let collection;
211211

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

@@ -233,11 +233,11 @@ describe('next CollectionView Data', function() {
233233
});
234234
});
235235

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

240-
myCollectionView = new MyCollectionView({ collection, sortWithCollection: false });
240+
myCollectionView = new MyCollectionView({ collection, viewComparator: false });
241241
myCollectionView.render();
242242
});
243243

0 commit comments

Comments
 (0)