From 6d0b325f7f5b9c1f3cfac9b73c6cd5fc3d1e2af0 Mon Sep 17 00:00:00 2001 From: R. Merkert Date: Tue, 16 Apr 2013 20:09:43 -0400 Subject: fix(angular): do not copy $$hashKey in copy/extend functions. Copying the $$hashKey as part of copy/extend operations makes little sense since hashkey is used primarily as an object id, especially in the context of the ngRepeat directive. This change maintains the existing $$hashKey of an object that is being copied into (likewise for extend). It is not uncommon to take an item in a collection, copy it, and then append it to the collection. By copying the $$hashKey, this leads to duplicate object errors with the current ngRepeat. Closes #1875 --- test/AngularSpec.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'test') diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 7f1ab8a3..0e5017ad 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -98,6 +98,63 @@ describe('angular', function() { src = dst = [2, 4]; expect(function() { copy(src, dst); }).toThrow("Can't copy equivalent objects or arrays"); }); + + it('should not copy the private $$hashKey', function() { + var src,dst; + src = {}; + hashKey(src); + dst = copy(src); + expect(hashKey(dst)).not.toEqual(hashKey(src)); + }); + + it('should retain the previous $$hashKey', function() { + var src,dst,h; + src = {}; + dst = {}; + // force creation of a hashkey + h = hashKey(dst); + hashKey(src); + dst = copy(src,dst); + + // make sure we don't copy the key + expect(hashKey(dst)).not.toEqual(hashKey(src)); + // make sure we retain the old key + expect(hashKey(dst)).toEqual(h); + }); + }); + + describe("extend", function() { + + it('should not copy the private $$hashKey', function() { + var src,dst; + src = {}; + dst = {}; + hashKey(src); + dst = extend(dst,src); + expect(hashKey(dst)).not.toEqual(hashKey(src)); + }); + + it('should retain the previous $$hashKey', function() { + var src,dst,h; + src = {}; + dst = {}; + h = hashKey(dst); + hashKey(src); + dst = extend(dst,src); + // make sure we don't copy the key + expect(hashKey(dst)).not.toEqual(hashKey(src)); + // make sure we retain the old key + expect(hashKey(dst)).toEqual(h); + }); + + it('should work when extending with itself', function() { + var src,dst,h; + dst = src = {}; + h = hashKey(dst); + dst = extend(dst,src); + // make sure we retain the old key + expect(hashKey(dst)).toEqual(h); + }); }); describe('shallow copy', function() { -- cgit v1.2.3