-
Notifications
You must be signed in to change notification settings - Fork 27.4k
angular.copy fails for objects implementing a forEach that depends on some internal state. #10304
Comments
@jelbourn yeh, this is unfortunate.... Are you bumping into this issue while invoking The current way of coping objects is not ideal and it fails for other "special" objects (ex.: #10210). What would be good, maybe, is to have some way of specifying a cloning objects (instead of creating a new regular object). I guess that recognizing But yeh, if you are invoking |
How about checking is We might miss some very rare cases where non-array objects do have a proper |
@gkalpak Given that the new ES6 Set and Map objects fall into that category, this doesn't seem like a very feature compatible approach. |
@realityking: When ES6 is supported by 1.x we can add checks for the specific types. @pkozlowski-opensource: Too lazy to look that up 😇 |
Yes, agree, this is more a question of how to clone objects where creating a new instance with the same prototype is not enough. Or more generally a questions of limits of the built-in |
@pkozlowski-opensource We ran into this directly using angular.copy after upgrading to 1.3. Our project has a workaround, but I imagine other Googlers will probably encounter this due to the prevalent use of Closure. |
@jelbourn Ok, thnx for clarifying. Can you think of a reasonable fix here (without explicitly testing for closure classes)? I must say that I'm short of ideas that would work in the general case.... |
I should have mentioned this earlier: the example we ran into was that we had an object that, deeply nested in its properties, had a goog.struct.Map that came from another library we depend on. The call to angular.copy is used in our wrapper service for $resource, which clones anything being saved and runs a pre-save transformation on the clone before sending the request. So it's a case of angular.copy running on any arbitrary data model. As for a fix,
Because of these two things, it can't be safe to use angular.forEach inside of angular.copy. What would be wrong with doing a manual iteration over the object keys? Doesn't work for ECMA6 collections? |
angular.forEach
attempts to use an object's ownforEach
function if it is available.angular.copy
usesObject.create
to make a new empty destination object with the same prototype as the source.angular.copy
then usesangular.forEach
to iterate over the keys of the destination object and delete any properties.The problem: if a class implements it's own forEach, but that forEach depends on some class property normally initialized in its constructor, then this iteration over the destination's keys will result in an error.
Real example: The Google Closure
goog.struct.Map
. This implements its own forEach, but it that forEach depends on thekeys_
property to have been initialized in the constructor. Other collections ingoog.struct
probably have the same problem.Running contrived example: http://jsbin.com/qumajeyuse/1/edit?js,console
Running example with
goog.struct.Map
: http://jsbin.com/fuvaliboli/2/edit?js,consoleThe text was updated successfully, but these errors were encountered: