最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I add new attributes in jointjs element? - Stack Overflow

programmeradmin7浏览0评论

I want to create a custom element with new attributes, I created my custom element like that but I need a new attribute to store information about the element.

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)

Thanks!

I want to create a custom element with new attributes, I created my custom element like that but I need a new attribute to store information about the element.

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)

Thanks!

Share Improve this question asked May 30, 2014 at 17:31 csadancsadan 2911 gold badge3 silver badges13 bronze badges 2
  • Can you provide more information about the extra information that you want to store? – Ryan G Commented May 30, 2014 at 17:51
  • a vector of strings ['1','2','3'....] – csadan Commented May 30, 2014 at 22:25
Add a comment  | 

2 Answers 2

Reset to default 13

You can add new properties next to the type and attrs. These will be your default properties for your element, like so:

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)

Later when you instantiate your element, you can also add properties only to that specific element:

var myNewRect = new joint.shapes.basic.newRect({ position: { x: 1, y: 1 }});
myNewRect.set('mycustom2', 'bar')
myNewRect.get('mycustom') // 'foo'
myNewRect.get('mycustom2') // 'bar'

All these properties will be taken into account when serializing the graph as well.

You can also use the provided Element#prop. See http://jointjs.com/api#joint.dia.Element:prop

发布评论

评论列表(0)

  1. 暂无评论