简体   繁体   中英

How to read value from prop in JointJS element?

I try to read a prop from a JointJS element. I can see that it gets added but I can't figure out how to read the value.

var uml = joint.shapes.uml;

var state = new uml.State({
  name: "some name",
  prop: { myId: "some id" },
  events: ["event1", "event2"],
  attrs: {
    ".uml-state-body": {
      fill: "rgba(48, 208, 198, 0.1)",
      stroke: "rgba(48, 208, 198, 0.5)",
      "stroke-width": 1.5
    },
    ".uml-state-separator": {
      stroke: "rgba(48, 208, 198, 0.4)"
    }
  }
});

this.graph.addCell(state);

this does not work

this.paper.on("element:pointerup ", function(elementView) {
  var id = elementView.model.get("myId");
});

you are really close... ;)

solution with actual code

I used prop in getter to have full path get("prop").myId

than works your: elementView.model.get("prop").myId

https://codepen.io/Michal-Miky-Jankovsky/pen/PVXaZy

why?

all object keys from constructor are "attributes" for getter

better solution...

you can set "myId" directly on model:

var state = new uml.State({
   myId: "some id",
   ...
});

than will be available for getter as expected:

elementView.model.get("myId")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM