动态地将一个端口添加到jointjs元素

时间:2016-02-09 21:49:07

标签: svg jointjs

所以,我在晚上的大部分时间都与这场比赛作斗争,我承认失败了。我在这个论坛上问这个问题,知道有人会说'#34;看这里"这可以让我节省几个小时:))

想到我可以在joint.shapes.devs.Model.extend中添加一个函数(如下所示:

addPort:function(name){       var portsArray = this.attributes.outPorts;       portsArray.push(名称);   }

但显然不是

我已经对演示进行了搜索并找不到任何结果

所以,问题::

如何向现有元素添加新端口?

我也问过关于联合谷歌小组的问题,但那里的流量很少,我希望SO就是这个地方;)

感谢

1 个答案:

答案 0 :(得分:0)

请使用set更新属性,而不是直接修改属性。 这很重要,因为模型可以触发事件,视图可以处理它(渲染端口)。

addPort: function(port) {
     // It is also important to clone the array here
     // as we do not want do modify the original attribute value.
     // Setting the same value on the model would trigger no event.
     /*
       var m = new Backbone.Model({ b: [] });
       var array = m.get('b');
       array.push('d');
       m.set('b', array); // -> no event triggered, m.get('b') === array
     */
     var ports = (this.get('outPorts') || []).slice();
     ports.push(port);
     this.set('outPorts', ports);
}