Search code examples
javascriptparse-platformparse-server

Clone Parse Server object


I have a Parse object (well, an array of parse objects). I want to make a clone of the objects but everything I try fails...in that changing the original object attributes also changes the clone's corresponding attribute. I have tried using Parse.Object.clone(), creating new array of objects, changing to JSON and then doing a deep clone but nothing works. After some research I came across this and this but this does offer a real solution.

Is there no good way to clone Parse objects and have the attributes be completely separate??

I want to essentially have a 'cancel' changes button which would revert to the cloned versions and not save.


Solution

  • Parse.Object.clone returns a shallow copy. For a deep copy (completely independant objects) I've written and used this code:

    var originalObject = ...
    var objectJSON = originalObject.toJSON();
    delete objectJSON.objectId; // force it to be a new DB object if you save it
    var twin = new Parse.Object( object.className );
    twin.set( objectJSON );
    

    In my opinion a Parse.Object.deepClone method would be nice...