使用自定义属性扩展到对象的结构js将丢失默认属性 [英] Fabric js extending toObject with custom properties, loses default ones

查看:93
本文介绍了使用自定义属性扩展到对象的结构js将丢失默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Befor张贴这个我一直在这里和其他许多地方一样,但我可以完全开始工作。

Befor posting this I've been looking up in here as in many other places, but I can get to have this working fully.

我需要的只是能够在所有形状中保存一些自定义属性。属性是:uuid和rt_attributes。

All I need is to be able to save some custom properties in all the shapes. The properties are : uuid, and rt_attributes.

因此,通过手册我添加了这段代码:

Therefore, as by manual I added this code :

 fabric.Object.prototype.toObject = (function(toObject) {
  console.log(toObject)
  return function() {
    return fabric.util.object.extend(toObject.call(this), {
      uuid: this.uuid,
      rt_attributes: this.rt_attributes
    });
  };
})(fabric.Object.prototype.toObject);

在某种程度上可以正常工作。

which does work fine, up to a certain degree.

当我在json中序列化并加载时,问题就来了。
自定义属性所在,但像IText这样的形状会引发异常,例如:

the problem comes when I serialize in json and load back. the custom attributes are in , but shapes like the IText raise an exception such as :

fabric.js:22199 Uncaught TypeError: Cannot read property 'split' of undefined

查看转储的json我可以看到。文本属性未导出。所以我担心覆盖toObject会失去默认对象的一些自定义属性。

Looking into the dumped json I can see the .text attribute is not exported. So My fear is that overriding the toObject I lose some of the custom attributes of the default object.

当然我可以在我的toObject函数中重新定义它,并删除所有缺少的修剪,但我认为

Of course I can redefine it in my toObject function with all the missing trims, but I though that

fabric.util.object.extend

会为我做这件事。

有人能指出我做错了吗?
谢谢。
L。

Can someone point me on what I'm doing wrong ? thanks. L.

ps
这里是结果json的片段:

p.s here is a snippet of the outcome json:

{"type":"i-    text","originX":"left","originY":"top","left":29,"top":677,"width":107,"height":22.6,"fill":"rgba(255,255,255,1)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"uuid":"04af6ab4-4eb1-432b-f46a-93b11a92292d","rt_attributes":[["fontFamily","text"],["fontSize","int"],["fill","color"],["opacity","float"],["top","int"],["left","int"]],"styles":{}},

你可以看到没有文字字段。但是uuid和rt_attributes都在。

as you can see there's no text field., but uuid and rt_attributes are in.

推荐答案

终于找到了正确的方法:

Finally found the proper way:

fabric.Object.prototype.toObject = (function (toObject) {
    return function (propertiesToInclude) {
        propertiesToInclude = (propertiesToInclude || []).concat(
          ['uuid','rt_attributes']
        );
        return toObject.apply(this, [propertiesToInclude]);
    };
})(fabric.Object.prototype.toObject);

这篇关于使用自定义属性扩展到对象的结构js将丢失默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆