如何从 JointJS 元素中的 prop 读取值?

How to read value from prop in JointJS element?

我尝试从 JointJS 元素中读取道具。我可以看到它已添加,但我无法弄清楚如何读取该值。

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.paper.on("element:pointerup ", function(elementView) {
  var id = elementView.model.get("myId");
});

你真的很接近...;)

具有实际代码的解决方案

我在 getter 中使用了 prop 以获得完整路径 get("prop").myId

比你的作品:elementView.model.get("prop").myId

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

为什么?

构造函数中的所有对象键都是getter

的“属性”

更好的解决方案...

您可以直接在型号上设置“myId”:

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

比预期的 getter 可用:

elementView.model.get("myId")