Jtopo 正确的添加自定义属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39759115/article/details/80015317

在使用 Jtopo 的过程中,有时候我们需要自定义一些属性来帮我们完成某些功能

在最开始的时候,我是直接添加的自定义属性:

 LockNode = new JTopo.Node();
 LockNode.myType = "device";   // 自定义属性

这样直接添加在使用时好像也什么毛病…

console.log(LockNode.myType);  // "device"

但是Jtopo 在初始化这个节点的时候是不包括这个属性的:

             this.initialize = function() { // node 初始化
                b.prototype.initialize.apply(this, arguments),
                    this.elementType = "displayElement",
                    this.x = 0,
                    this.y = 0,
                    this.width = 32,
                    this.height = 32,
                    this.visible = !0,
                    this.alpha = 1,
                    this.rotate = 0,
                    this.scaleX = 1,
                    this.scaleY = 1,
                    this.strokeColor = "22,124,255",
                    this.borderColor = "22,124,255",
                    this.fillColor = "22,124,255",
                    this.shadow = !1,
                    this.shadowBlur = 5,
                    this.shadowColor = "rgba(0,0,0,0.5)",
                    this.shadowOffsetX = 3,
                    this.shadowOffsetY = 6,
                    this.transformAble = !1,
                    this.zIndex = 0;
                var a = "x,y,width,height,visible,alpha,rotate,scaleX,scaleY,strokeColor,fillColor,shadow,shadowColor,shadowOffsetX,shadowOffsetY,transformAble,zIndex".split(",");
                this.serializedProperties = this.serializedProperties.concat(a)
            },
            this.initialize(),


            ...


            this.initialize = function(c) {
                b.prototype.initialize.apply(this, arguments),
                    this.elementType = "node",
                    this.zIndex = a.zIndex_Node,
                    this.text = c,
                    this.font = "12px Consolas",
                    this.fontColor = "255,255,255",
                    this.borderWidth = 0,
                    this.borderColor = "255,255,255",
                    this.borderRadius = null,
                    this.dragable = !0,
                    this.textPosition = "Bottom_Center",
                    this.textOffsetX = 0,
                    this.textOffsetY = 0,
                    this.transformAble = !0,
                    this.inLinks = null,
                    this.outLinks = null;
                var d = "text,font,fontColor,textPosition,textOffsetX,textOffsetY,borderRadius".split(",");
                this.serializedProperties = this.serializedProperties.concat(d)
            },
                this.initialize(c),

上面的代码中,我们可以看到初始化节点的所有属性后,Jtopo 把所有的属性都以数组的形式放在了节点的 serializedProperties 属性中

so,我们自定义属性的正确的添加方式应该是这样的:

            node.myType = "device";    // 在创建节点的时候添加自定义属性   
            node.serializedProperties.push("myType"); // 把自定义属性加入进节点的属性组 serializedProperties 中

            console.log(node.myType); // "device"

通过这种方式,我们在调用 Jtopo 自身一些其他的方法时,如使用 toJson() 导出数据时,就能看的我们自定义的属性

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_39759115/article/details/80015317