简体   繁体   English

在 Javascript 中保存和恢复对象?

[英]Saving and Restoring Objects In Javascript?

I am creating a basic web-based diagramming tool using Javascript and the Raphael Library to create the shapes.我正在使用 Javascript 和 Raphael 库创建一个基本的基于 Web 的图表工具来创建形状。 I would now like to know how to save and restore objects that i have created.我现在想知道如何保存和恢复我创建的对象。

Belows code creates a basic shape, and enables dragging.下面的代码创建一个基本形状,并启用拖动。 How would i go about being able to save and restore the objects such as this:我如何能够保存和恢复这样的对象:

var cubeD = paper.rect(400, 400, 50, 50);
    cubeD.attr({"fill":"yellow"});
    cubeD.draggable.enable();
    cubeD.name = 'cubeD';
objArray.push(cubeD);
objArray_txt.push(cubeD.name);
objAssoc['cubeD'] = cubeD;

I tried JSON to store the object but the following code doesnt seem to work:我尝试了 JSON 来存储 object 但以下代码似乎不起作用:

var myJSON = JSON.encode(cubeD);

But i get the error但我得到了错误

TypeError: cyclic object value类型错误:循环 object 值

What would more experienced developers suggest?更有经验的开发人员会提出什么建议?

https://github.com/douglascrockford/JSON-js https://github.com/douglascrockford/JSON-js

The cycle.js file allows cyclic references. cycle.js文件允许循环引用。

Then you just need to use those functions to avoid your error and just use the so-called "normal" JSON methods to save and restore your object (ask if you can't figure how).然后你只需要使用这些函数来避免你的错误,只需使用所谓的“正常” JSON 方法来保存和恢复你的 object (询问你是否无法弄清楚如何)。 When using the "normal" JSON method to restore the object, don't forget to give a new instance of the constructor of your object as argument so that it has all the methods you need and behave as you expect (because JSON only saves data, not functions and therefore, it can't store the Constructor). When using the "normal" JSON method to restore the object, don't forget to give a new instance of the constructor of your object as argument so that it has all the methods you need and behave as you expect (because JSON only saves data ,而不是函数,因此它不能存储构造函数)。

You can't bank on being able to serialize an object made by an external framework.您不能指望能够序列化由外部框架制作的 object。 Either that API supports it or it doesn't (I'm not familiar with Raphael). API 要么支持,要么不支持(我不熟悉 Raphael)。

The way to solve this is to have an intermediate format which is probably a plain chunk of JSON, and serialize that.解决这个问题的方法是有一个中间格式,它可能是 JSON 的普通块,并将其序列化。 This contains the spec to your object.这包含 object 的规范。 A method would call your API to build the object from your JSON object.一种方法会调用您的 API 从您的 JSON ZA8CFDE6331BD59EB2AC96F8911C46.B 构建 object。 This would be called after deserialization.这将在反序列化后调用。

I use this library on most of my projects: https://github.com/douglascrockford/JSON-js我在我的大部分项目中都使用这个库: https://github.com/douglascrockford/JSON-js

It has a $.toJSON method that will serialize/deserialize javascript into a string.它有一个$.toJSON方法,可以将 javascript 序列化/反序列化为字符串。 I'm not sure if this is the best approach but it certainly works for us.我不确定这是否是最好的方法,但它肯定对我们有用。 I put the JSON in a cookie and read from it as well.我将 JSON 放入 cookie 中并从中读取。

Try this Raphael SketchPad试试这个拉斐尔画板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM