2009-07-15 52 views
1

我有兩個dojo.dnd.Sources項目。每當一件物品被丟棄時,我需要使用xhr在物品中堅持物品的新訂單。Dojo:拖放完成後是否有事件

在dnd操作完成(成功)後是否有dojo事件或主題被觸發?什麼是使用它的最好方法?

回答

3

大概我不明白在所有細節中的問題,但我不明白爲什麼你需要處理事件或主題。記錄更改的最佳方式是攔截相關源的更新方法。具體而言,您需要攔截insertNodes()進行丟棄或任何其他添加。

簡單的例子(僞代碼):

var source1, source2; 
// ... 
// initialize sources 
// populate sources 
// ... 

function getAllItems(source){ 
    var items = source.getAllNodes().map(function(node){ 
    return source.getItem(node.id); 
    }); 
    return items; 
} 

function dumpSource(source){ 
    var items = getAllItems(source); 
    // XHR items here to your server 
} 

function recordChange(){ 
    // now we know that some change has occured 
    // it could be a drop or some programmatic updates 
    // we don't really care 
    dumpSource(source1); 
    dumpSource(source2); 
} 

dojo.connect(source1, "insertNodes", recordChanges); 
dojo.connect(source2, "insertNodes", recordChanges); 
// now any drop or other change will trigger recordChanges() 
// after the change has occurred. 

你可以自作聰明有關,而是發送整個列表的一些差異的信息,但它是由你來生成它—你有你需要的一切。

1

可以使用dojo.subscribe當滴完像這樣做一些事情:

dojo.subscribe("/dnd/drop", function(source, nodes, copy, target) { 
    // do your magic here 
}); 

有使用訂閱的dojotoolkit tests site的例子。有關dojo publishsubscribe的更多信息。

或者,您可以連接到onDndDrop方法。

var source = new dojo.dnd.Source(...); 
dojo.connect(source, "onDndDrop", function(source, nodes, copy, target) { 
    // make magic happen here 
}); 

連接方法在最後調用,所以項目將在那裏。

+0

被丟棄的元素呢,如果我使用 「/ DND /下降」 尚未到來。它仍然在源頭上。我發現了一些使用setTimeout來做我想要的東西的魔法。 – Wienczny 2009-07-16 01:36:45

0

我將這個注意力放在道場樹人跟我一樣,會碰到這個問題。這裏給出的解決方案在我的情況下效果不好。我在Dojo樹中使用了dijit.tree.dndSource,並且訂閱「/ dnd/drop」允許我捕獲該事件,即使此時我的基礎數據存儲尚未使用最新更改進行更新。所以我試着等Wienczny解釋說,這並不能完全解決問題,因爲我不能依靠暫停來做等待工作。商店更新所花費的時間可能會有所不同,即更短或更長取決於數據結構的複雜程度。我找到了重寫dndController的onDndDrop方法的解決方案。只需在樹初始化中指定onDndDrop即可:有一件事我覺得很奇怪,雖然你不能搭配這種方法,但在dnd期間你會得到奇怪的行爲。

   this._tree = new MapConfigTree({ 
       checkAcceptance: this.dndAccept, 
       onDndDrop: this.onDndDrop, 
       betweenThreshold:5, 

方法

  onDndDrop : function(source, nodes, copy, target){ 
      if(source.dropPosition === 'Over' && (target.targetAnchor.item.type[0] == 'Test layer')) { 
       this.inherited(arguments); 
       // do your bit here 
      } else { 
       this.onDndCancel(); 
      } 
     }