diff options
| author | Igor Minar | 2012-01-05 23:03:45 -0800 | 
|---|---|---|
| committer | Igor Minar | 2012-01-06 12:19:39 -0800 | 
| commit | cd9a7b9608707c34bec2316ee8c789a617d22a7b (patch) | |
| tree | 407b7260e8159fe0c9aeaf301e067a7f9a5b1149 | |
| parent | 1dccaaaaa27a4db91d5271438688bc96e199e561 (diff) | |
| download | angular.js-cd9a7b9608707c34bec2316ee8c789a617d22a7b.tar.bz2 | |
fix(ng:repeat): support repeating over array with null
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
| -rw-r--r-- | src/apis.js | 10 | ||||
| -rw-r--r-- | test/ApiSpecs.js | 19 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 8 | 
3 files changed, 34 insertions, 3 deletions
| diff --git a/src/apis.js b/src/apis.js index 6f9b1d0a..7b470802 100644 --- a/src/apis.js +++ b/src/apis.js @@ -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;  } diff --git a/test/ApiSpecs.js b/test/ApiSpecs.js index a0833fba..35a85bd4 100644 --- a/test/ApiSpecs.js +++ b/test/ApiSpecs.js @@ -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']); +    });    });  }); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index d2867d09..3b53c7d2 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -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; | 
