2015-10-18 20 views
1

通過點擊紙的空白空間的新細胞將被添加在紙的每一個細胞位置事件:變化:使用joint.js

paper.on({ 
    'blank:pointerdblclick': function(event, x, y) { 
     var rect = new joint.shapes.app.Standard({ 
      position: { x: x, y: y }, 
      size: { width: 100, height: 80 }, 
     }); 

     graph.addCell(rect); 

     rect.on('change:position', function() { 
      paper.fitToContent(); 
     }); 
    } 
}); 

但隨着change:position事件在blank:pointerdblclick設置事件,它只是爲新的元素着想。但是應該爲本文提供的所有單元設置。 因此,爲了移動紙上的任何單元格,應該完成fitToContent()函數...

我該怎麼做?

P.S:標準元素的定義如下:

joint.shapes.app.Standard = joint.shapes.basic.Generic.extend({ 
    markup: '<rect/><circle class="port-top"/><circle class="port-bottom"/><circle class="port-right"/><circle class="port-left"/><text/>', 
    defaults: joint.util.deepSupplement({ 
     type: 'app.Standard', 
     attrs: { 
      ... 
     } 
    }, joint.shapes.basic.Generic.prototype.defaults) 
}); 

回答

2

使用graph.getElements()並在它們之間迭代設置處理

graph.getElements().forEach(function(element) { 
    element.on('change:position', function() { 
     paper.fitToContent(); 
    });