我想创建一个带有新属性的自定义元素,我创建了这样的自定义元素,但我需要一个新属性来存储有关该元素的信息。
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)
谢谢!
最佳答案
您可以在 type
和 attrs
旁边添加新属性。这些将是您元素的默认属性,如下所示:
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)
稍后当您实例化您的元素时,您还可以仅向该特定元素添加属性:
var myNewRect = new joint.shapes.basic.newRect({ position: { x: 1, y: 1 }});
myNewRect.set('mycustom2', 'bar')
myNewRect.get('mycustom') // 'foo'
myNewRect.get('mycustom2') // 'bar'
在序列化图形时也会考虑所有这些属性。
关于javascript - 我可以在 jointjs 元素中添加新属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960312/