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

angular.copy() doesn't strip $$hashKey #1875

Closed
zivester opened this issue Jan 24, 2013 · 5 comments
Closed

angular.copy() doesn't strip $$hashKey #1875

zivester opened this issue Jan 24, 2013 · 5 comments

Comments

@zivester
Copy link

angular.copy({ name: "DEF", $$hashKey: "089" });
=> Object { name= "DEF", $$hashKey= "089" }

I believe it is suppose to strip out $$hashKey, like:

angular.fromJson(angular.toJson({ name: "DEF", $$hashKey: "089" }));
=> Object { name= "DEF" }

Why aren't the two equivalent?

@petebacondarwin
Copy link
Member

I guess it probably should since the idea is that each object has a unique $$hashKey and now you have to potentially diverging objects with the same key.
@IgorMinar & @mhevery - Any thoughts on this?

@spamdaemon
Copy link

With the new change to ng-repeat 1.1.4 that prevents duplicates, this break my code.

My use-case is a simple array, where the user can drag a cell onto another cell to copy it. Now the copy creates a duplicate because the key is copied as well.

For now, I'm just overriding angular.copy with my own version that deletes the hashkey, because there are just too many places in my code where I copy objects.

BTW: extend() also copies the hashkey.

@spamdaemon
Copy link

I've just commented on that patch, and it occurred to me that the use of hashKey() is kind of confused. First, hashKey() is a misnomer, because

  • the documentation says "...Computes a hash of an 'obj'..."
  • the resulting hash value is used as if two objects with the same hash are equal.

It is true that objects that are equal must have the same hash value, but the reverse is not true.
A more appropriate function would be getObjectID or assignObjectID.

Since the "hash" is really used as an object identity value, it makes no sense to copy() or extend() it to another object, ever.

I kind of think that this needs to be addressed, because it will generate subtle problems in any larger application that will be difficult to track down.

I think I have an idea for a patch that might work and should be less intrusive (i.e. not drop all $ properties).

petebacondarwin pushed a commit that referenced this issue May 8, 2013
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
@srikanthbattula
Copy link

For better understanding on angular.copy() visit this article
http://sharepoint-2010-world.blogspot.in/2013/10/angularcopy-in-angularjs.html

@bostondevin
Copy link

Just use angular.toJson(obj) to get rid of the $$hashKey's

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants