Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 759
SimpleOne Avatar asked Mar 10 '23 01:03

SimpleOne


2 Answers

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...

like image 83
Ilya Avatar answered Mar 20 '23 00:03

Ilya


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.

like image 35
Jake T. Avatar answered Mar 20 '23 01:03

Jake T.