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.
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...
What's happening with Parse.Object.clone()? That seems like it should be what you want.
the iOS (and likely the android) SDK has a revert method on objects to reset to the last time it was saved / fetched.
Keep in mind that for objects in javascript, passing them into functions treats them as pass by reference, more or less, so changes within a function will change the object passed in. Sometimes useful and sometimes annoying.
If Parse.Object.clone() isn't working, My next suggestion, while annoying, would be to create a new object shell and fetch it / query for the object if you need includes when you need to "reset" the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With