Skip to content

Commit

Permalink
Merge pull request #12208 from Batterii/computed-sort-fix
Browse files Browse the repository at this point in the history
[BUGFIX release] Ember.computed.sort was crashing when it hit a null value. Fixes #12207.
  • Loading branch information
mixonic committed Aug 26, 2015
2 parents 312617d + 579bb88 commit a09587b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ function propertySort(itemsKey, sortPropertiesKey) {
var items = itemsKey === '@this' ? this : get(this, itemsKey);
var sortProperties = get(this, sortPropertiesKey);

if (items === null || typeof items !== 'object') { return Ember.A(); }

// TODO: Ideally we'd only do this if things have changed
if (cp._sortPropObservers) {
cp._sortPropObservers.forEach(args => removeObserver.apply(null, args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
union,
intersect
} from 'ember-runtime/computed/reduce_computed_macros';
import { isArray } from 'ember-runtime/utils';

var obj;
QUnit.module('map', {
Expand Down Expand Up @@ -1089,6 +1090,17 @@ QUnit.test('property paths in sort properties update the sorted array', function
deepEqual(obj.get('sortedPeople'), [cersei, jaime, sansa], 'array is sorted correctly');
});

QUnit.test('if the dependentKey is neither an array nor object, it will return an empty array', () => {
set(obj, 'items', null);
ok(isArray(obj.get('sortedItems')), 'returns an empty arrays');

set(obj, 'array', undefined);
ok(isArray(obj.get('sortedItems')), 'returns an empty arrays');

set(obj, 'array', 'not an array');
ok(isArray(obj.get('sortedItems')), 'returns an empty arrays');
});

function sortByLnameFname(a, b) {
var lna = get(a, 'lname');
var lnb = get(b, 'lname');
Expand Down

0 comments on commit a09587b

Please sign in to comment.