在 Javascript 中保存和恢复对象?

发布于 2024-11-04 14:21:10 字数 546 浏览 3 评论 0原文

我正在使用 Javascript 和 Raphael 库创建一个基于 Web 的基本图表工具来创建形状。我现在想知道如何保存和恢复我创建的对象。

下面的代码创建一个基本形状,并启用拖动。我将如何保存和恢复这样的对象:

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;

我尝试使用 JSON 来存储对象,但以下代码似乎不起作用:

var myJSON = JSON.encode(cubeD);

但我收到错误

类型错误:循环对象值

更有经验的开发人员会建议什么?

I am creating a basic web-based diagramming tool using Javascript and the Raphael Library to create the shapes. 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:

var myJSON = JSON.encode(cubeD);

But i get the error

TypeError: cyclic object value

What would more experienced developers suggest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俏︾媚 2024-11-11 14:21:10

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

cycle.js 文件允许循环引用。

然后,您只需要使用这些函数来避免错误,并使用所谓的“正常”JSON 方法来保存和恢复您的对象(如果您不知道如何操作,请询问)。当使用“普通”JSON 方法恢复对象时,不要忘记提供对象构造函数的新实例作为参数,以便它具有您需要的所有方法并按您期望的方式运行(因为 JSON 只保存数据,而不是函数,因此它无法存储构造函数)。

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

The cycle.js file allows cyclic references.

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

≈。彩虹 2024-11-11 14:21:10

您不能指望能够序列化外部框架创建的对象。该 API 要么支持它,要么不支持(我对 Raphael 不熟悉)。

解决这个问题的方法是使用一种中间格式(可能是一段普通的 JSON),然后将其序列化。这包含您的对象的规格。方法将调用您的 API 以从 JSON 对象构建对象。这将在反序列化后调用。

You can't bank on being able to serialize an object made by an external framework. Either that API supports it or it doesn't (I'm not familiar with Raphael).

The way to solve this is to have an intermediate format which is probably a plain chunk of JSON, and serialize that. This contains the spec to your object. A method would call your API to build the object from your JSON object. This would be called after deserialization.

撩心不撩汉 2024-11-11 14:21:10

我在我的大多数项目中都使用这个库:
https://github.com/douglascrockford/JSON-js

它有一个 $. toJSON 方法,将 javascript 序列化/反序列化为字符串。我不确定这是否是最好的方法,但它确实对我们有用。我将 JSON 放入 cookie 中并从中读取。

I use this library on most of my projects:
https://github.com/douglascrockford/JSON-js

It has a $.toJSON method that will serialize/deserialize javascript into a string. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文