Javascript 我可以在jointjs元素中添加新属性吗?

Javascript 我可以在jointjs元素中添加新属性吗?,javascript,jointjs,Javascript,Jointjs,我想创建一个具有新属性的自定义元素,我创建了这样的自定义元素,但我需要一个新属性来存储有关该元素的信息 joint.shapes.basic.newRect = joint.shapes.basic.Generic.extend({ markup: '<g class="rotatable"><g class="scalable"><rect/></g><text/></g>', defaults: joint.util

我想创建一个具有新属性的自定义元素,我创建了这样的自定义元素,但我需要一个新属性来存储有关该元素的信息

joint.shapes.basic.newRect = joint.shapes.basic.Generic.extend({

markup: '<g class="rotatable"><g class="scalable"><rect/></g><text/></g>',

defaults: joint.util.deepSupplement({

    type: 'basic.newRect',
    attrs: {
        'rect': { fill: 'white', stroke: 'black', 'follow-scale': true, width: 80, height: 40 },
        'text': { 'font-size': 14, 'ref-x': .5, 'ref-y': .5, ref: 'rect', 'y-alignment': 'middle', 'x-alignment': 'middle' }
    }

}, joint.shapes.basic.Generic.prototype.defaults)
joint.shapes.basic.newRect=joint.shapes.basic.Generic.extend({
标记:“”,
默认值:joint.util.deepaddress({
键入:“basic.newRect”,
属性:{
'rect':{fill:'white',stroke:'black',follow scale:'true,宽度:80,高度:40},
'text':{'font size':14,'ref-x':.5,'ref-y':.5,ref:'rect','y-alignment':'middle','x-alignment':'middle'}
}
},joint.shapes.basic.Generic.prototype.defaults)

谢谢!

您可以在
类型
属性
旁边添加新属性。这些将是元素的默认属性,如下所示:

joint.shapes.basic.newRect = joint.shapes.basic.Generic.extend({

markup: '<g class="rotatable"><g class="scalable"><rect/></g><text/></g>',

defaults: joint.util.deepSupplement({

    type: 'basic.newRect',
    attrs: {
        'rect': { fill: 'white', stroke: 'black', 'follow-scale': true, width: 80, height: 40 },
        'text': { 'font-size': 14, 'ref-x': .5, 'ref-y': .5, ref: 'rect', 'y-alignment': 'middle', 'x-alignment': 'middle' }
    },
    mycustom: 'foo'

}, joint.shapes.basic.Generic.prototype.defaults)

序列化图形时也将考虑所有这些属性。

您也可以使用提供的
元素#prop
。请参阅

您可以提供有关要存储的额外信息的更多信息吗?字符串向量['1'、'2'、'3'…]@dave在本例中我应该选择什么:
myNewRect.set('mycustom',foo')
或'myNewRect.prop('mycustom',foo')
?我如何删除这个元素,因为没有
removeProp()`?@user3142695 set()和prop()都适合平面属性。prop()的功能在于嵌套属性。el.prop('mycustom/nested/object/property',')。removeProp()很好!它确实存在,但不知何故我们在API文档中跳过了它。如果您下载并搜索“removeProp”,您会找到它。它的签名很简单:removeProp(路径[,opt])。
prop()
set()
之间的区别是什么?prop特定于自定义数据属性。
var myNewRect = new joint.shapes.basic.newRect({ position: { x: 1, y: 1 }});
myNewRect.set('mycustom2', 'bar')
myNewRect.get('mycustom') // 'foo'
myNewRect.get('mycustom2') // 'bar'