2016-02-09 34 views
1

因此,我在這個晚上的大部分時間都在與這場戰鬥,我承認失敗。我問的問題在這個論壇的知識,有人會說:「看這裏」,這可能有救了我幾個小時:)動態添加一個端口到一個jointjs元素

以爲我可以只添加一個功能到joint.shapes.devs。 Model.extend(像這樣:

添加端口:函數(名稱){VAR = portsArray this.attributes.outPorts; portsArray.push(名);}

,但顯然不是

我已經搜遍了演示和酸味CE無濟於事

所以,這個問題::

如何添加一個新的端口,以現有的元素?

我還要求對jointjs問題Google網上論壇,但對有這麼小的流量,我希望SO是地方;)

感謝

回答

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); 
}