Dojo dnd(拖放)1.7.2-如何维护一个单独的(非Dojo dnd)列表



我使用的是Dojo dnd 1.7.2版本,它通常运行得很好。我很高兴。

我的应用程序维护了许多项目阵列,当用户四处拖放项目时,我需要确保我的阵列得到更新,以反映用户看到的内容。

为了实现这一点,我认为我需要在Source前后运行一些代码。onDndDrop

如果我使用dojo.connect在我的Source上为onDndDroponDrop设置一个处理程序,那么我的代码似乎调用得太晚了。也就是说,传递给处理程序的source实际上不再包含该项。

这是一个问题,因为我想调用source.getItem(nodes[0].id)来获取被拖动的实际数据,这样我就可以在数组中找到它,并更新这些数组以反映用户正在进行的更改。

也许我做错了;还有更好的方法吗?

好的,我找到了一个很好的方法。在对另一个问题的回答中发现了一个提示:https://stackoverflow.com/a/1635554/573110

我成功的一系列通话基本上是:

var source = new dojo.dnd.Source( element, creationParams );
var dropHandler = function(source,nodes,copy){
  var o = source.getItem(nodes[0].id); // 0 is cool here because singular:true.
  // party on o.data ...
  this.oldDrop(source,nodes,copy);
}
source.oldDrop = source.onDrop;
source.onDrop = dropHandler;

这确保了onDropdropHandler)的新实现在先前安装的实现之前被调用。

我想,dndSource有几个不同的实现。但是,关于mouseover/dnddrop期间调用的事件/checkfunction,有一些事情需要了解。

一种方法是为您可能拥有的任何目标设置checkAcceptance(source, nodes)。然后保留当前拖动的节点的引用。但是,由于有多个容器具有动态内容,因此会变得棘手。

设置您的Source,同时覆盖checkAcceptance并使用已知的(可能是全局的)变量来跟踪。

var lastReference = null;
var target = dojo.dnd.Source(node, {
    checkAcceptance(source, nodes) : function() {
        // this is called when 'nodes' are attempted dropped - on mouseover
        lastReference = source.getItem(nodes[0].id)
        // returning boolean here will either green-light or deny your drop
        // use fallback (default) behavior like so:
        return this.inhertied(arguments);
    }
});

最好的方法可能是这样的-你手头有目标和源加节点,但你需要找出在哪个堆栈中查找节点是正确的。我相信它与你准备使用的事件(onDrop)同时发布:

dojo.subscribe("/dnd/drop", function(source, nodes, copy, target) {
  // figure out your source container id and target dropzone id
  // do stuff with nodes
  var itemId = nodes[0].id
}

此处列出了通过dojo.subscribe和事件提供的机制/主题http://dojotoolkit.org/reference-guide/1.7/dojo/dnd.html#manager

最新更新