克隆解析服务器对象 [英] Clone Parse Server object
问题描述
我有一个Parse对象(嗯,是一个parse对象的数组).我想对对象进行克隆,但是我尝试的所有操作都失败了……因为更改原始对象属性也会更改克隆的相应属性.我尝试使用Parse.Object.clone(),创建对象的新数组,更改为JSON,然后进行深度克隆,但无济于事.经过一番研究,我遇到了此, 此,但这确实提供了真正的解决方案.
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.
没有很好的方法来克隆Parse对象并使属性完全分开吗?
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
返回浅表副本.对于深层副本(完全独立的对象),我编写并使用了以下代码:
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 );
我认为Parse.Object.deepClone方法会很好...
In my opinion a Parse.Object.deepClone method would be nice...
这篇关于克隆解析服务器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!