Minerhaoxue
Minerhaoxue

Reputation: 13

JointjS: How to get attributes of a given element?

I have an element defined as this:

var m1 = new joint.shapes.devs.Model({
    position: { x: 100, y: 50 },
    size: { width: 190, height: 50 },
    inPorts: ['in'],
    outPorts: ['out'],
    attrs: {
        '.label': { text: 'Model','ref-x': .4, 'ref-y': .25 ,fill: '#fefefe',
            'font-size': 14,
            'font-weight': 'bold', 
            'font-variant': 'small-caps' },
        rect: { fill: '#fefefe'},
        '.inPorts circle': { r:5 ,fill: '#16A085' ,magnet: 'passive', type: 'input'},
        '.outPorts circle': { r:5, fill: '#E74C3C',magnet: 'passive',type: 'output' },
    }

THe question is how can I get the '.label' attribute? E.g, I need to get the text "Model", what should I do? If I want to get the 'fill' attr of 'rect' , I can simply use m1.get('attrs').rect.fill.

But I don't know HOW TO GET the '.label' attr.

Upvotes: 1

Views: 5223

Answers (2)

Faisal Zulfiqar
Faisal Zulfiqar

Reputation: 1

var rootnode = new joint.shapes.basic.Circle({
  position: { x: 20, y: 220 },
  size: { width: 60, height: 30 },
  attrs: {
    text: { text: 'parent' },
    circle: { fill: 'yellow', hasChildren:false }
  },
  name: 'parent'   
});

graph.addCell(rootnode);

Upvotes: 0

dave
dave

Reputation: 4403

Use the attr() method for both setting attributes and getting them back:

m1.attr('.label/text') // 'Model'
m1.attr('.label/text', 'New Model')
m1.attr('.label/text') // 'New Model'

'/' is a path separator into the nested attrs object.

Upvotes: 4

Related Questions