Skip to content

Commit

Permalink
Merge pull request #15733 from ilucin/fix-computed-sort-regression
Browse files Browse the repository at this point in the history
[BUGFIX] Fix computed sort regression when array prop initially null
  • Loading branch information
rwjblue authored Oct 13, 2017
2 parents f4bbfe2 + 2cf4219 commit 032a271
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,6 @@ function propertySort(itemsKey, sortPropertiesKey) {
activeObservers.forEach(args => removeObserver(...args));
}

let itemsKeyIsAtThis = (itemsKey === '@this');
let items = itemsKeyIsAtThis ? this : get(this, itemsKey);
if (!isArray(items)) { return emberA(); }

function sortPropertyDidChange() {
this.notifyPropertyChange(key);
}
Expand All @@ -757,6 +753,10 @@ function propertySort(itemsKey, sortPropertiesKey) {

activeObserversMap.set(this, activeObservers);

let itemsKeyIsAtThis = (itemsKey === '@this');
let items = itemsKeyIsAtThis ? this : get(this, itemsKey);
if (!isArray(items)) { return emberA(); }

return sortByNormalizedSortProperties(items, normalizedSortProperties);
}, { dependentKeys: [`${sortPropertiesKey}.[]`], readOnly: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,13 @@ QUnit.test('updating sort properties detaches observers for old sort properties'
], 'after changing removed item array is not updated');
});

QUnit.test('sort works if array property is null (non array value) on first evaluation of computed prop', function() {
obj.set('items', null);
deepEqual(obj.get('sortedItems'), []);
obj.set('items', emberA([{fname: 'Cersei', lname: 'Lanister'}]));
deepEqual(obj.get('sortedItems'), [{fname: 'Cersei', lname: 'Lanister'}]);
});

QUnit.test('updating sort properties updates the sorted array', function() {
deepEqual(obj.get('sortedItems').mapBy('fname'), [
'Cersei',
Expand Down

0 comments on commit 032a271

Please sign in to comment.