Skip to content

Commit

Permalink
Merge pull request #11220 from dsokolowski/computed_sort_fix
Browse files Browse the repository at this point in the history
Fix for issue with Em.computed.sort observers
  • Loading branch information
rwjblue committed May 19, 2015
2 parents 76f5665 + 164a80e commit c56f6b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ember-runtime/lib/computed/reduce_computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,13 @@ function ReduceComputedProperty(options) {
var previousDependentArray = meta.dependentArrays[dependentKey];

if (dependentArray === previousDependentArray) {

// The array may be the same, but our item property keys may have
// changed, so we set them up again. We can't easily tell if they've
// changed: the array may be the same object, but with different
// contents.
if (cp._previousItemPropertyKeys[dependentKey]) {
meta.dependentArraysObserver.teardownPropertyObservers(dependentKey, cp._previousItemPropertyKeys[dependentKey]);
delete cp._previousItemPropertyKeys[dependentKey];
meta.dependentArraysObserver.setupPropertyObservers(dependentKey, cp._itemPropertyKeys[dependentKey]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,34 @@ QUnit.module('computedSort - sortProperties', {

commonSortTests();

QUnit.test("updating sort properties detaches observers for old sort properties", function() {
var objectToRemove = get(obj, 'items').objectAt(3);

run(function() {
sorted = get(obj, 'sortedItems');
});

deepEqual(sorted.mapBy('fname'), ['Cersei', 'Jaime', 'Bran', 'Robb'], "precond - array is initially sorted");

run(function() {
set(obj, 'itemSorting', Ember.A(['fname:desc']));
});

deepEqual(sorted.mapBy('fname'), ['Robb', 'Jaime', 'Cersei', 'Bran'], "after updating sort properties array is updated");

run(function() {
get(obj, 'items').removeObject(objectToRemove);
});

deepEqual(sorted.mapBy('fname'), ['Robb', 'Jaime', 'Cersei'], "after removing item array is updated");

run(function() {
set(objectToRemove, 'lname', 'Updated-Stark');
});

deepEqual(sorted.mapBy('fname'), ['Robb', 'Jaime', 'Cersei'], "after changing removed item array is not updated");
});

QUnit.test("updating sort properties updates the sorted array", function() {
run(function() {
sorted = get(obj, 'sortedItems');
Expand Down

0 comments on commit c56f6b3

Please sign in to comment.