Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(ng:repeat): support repeating over array with null
Browse files Browse the repository at this point in the history
typeof null == 'object', but it doesn't behave like an object
because its properties can't be dereferenced, so we need
to special-case it.

Closes #702
  • Loading branch information
IgorMinar committed Jan 6, 2012
1 parent 1dccaaa commit cd9a7b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
* The resulting string key is in 'type:hashKey' format.
*/
function hashKey(obj) {
var objType = typeof obj;
var key = obj;
if (objType == 'object') {
var objType = typeof obj,
key;

if (objType == 'object' && obj !== null) {
if (typeof (key = obj.$$hashKey) == 'function') {
// must invoke on object to keep the right this
key = obj.$$hashKey();
} else if (key === undefined) {
key = obj.$$hashKey = nextUid();
}
} else {
key = obj;
}

return objType + ':' + key;
}

Expand Down
19 changes: 19 additions & 0 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ describe('api', function() {
expect(map.shift('key')).toEqual(undefined);
expect(map[hashKey('key')]).toEqual(undefined);
});

it('should support primitive and object keys', function() {
var obj1 = {},
obj2 = {};

var map = new HashQueueMap();
map.push(obj1, 'a1');
map.push(obj1, 'a2');
map.push(obj2, 'b');
map.push(1, 'c');
map.push(undefined, 'd');
map.push(null, 'e');

expect(map[hashKey(obj1)]).toEqual(['a1', 'a2']);
expect(map[hashKey(obj2)]).toEqual(['b']);
expect(map[hashKey(1)]).toEqual(['c']);
expect(map[hashKey(undefined)]).toEqual(['d']);
expect(map[hashKey(null)]).toEqual(['e']);
});
});
});

8 changes: 8 additions & 0 deletions test/widgetsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@ describe('widget', function() {
expect(element.text()).toBe('a|b|||c||d|');
}));

it('should iterate over all kinds of types', inject(function($rootScope, $compile) {
var element = $compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope);
$rootScope.array = ['a', 1, null, undefined, {}];
$rootScope.$digest();

expect(element.text()).toMatch(/a\|1\|\|\|\{\s*\}\|/);
}));


describe('stability', function() {
var a, b, c, d, lis, element;
Expand Down

0 comments on commit cd9a7b9

Please sign in to comment.