javascript - KnockoutJS/jQuery UI 可排序冲突

标签 javascript jquery-ui dom knockout.js jquery-ui-sortable

我正在尝试同时使用 KnockoutJS 和 jQuery UI Sortable。我知道这已经完成了(特别是 knockout-sortable ),但我的用例有一些非常具体的行为,我希望避免尝试进行切换。

无论如何,问题非常简单——在使用 jQuery UI Sortable 移动 DOM 元素后,Knockout 在移除绑定(bind)到该 DOM 元素的 observableArray 元素时表现异常。它将无法移除被移动的元素,并且如果落入被移动元素位置的元素被移除,它将同时移除该元素和最初被移动的元素。难以用语言表达,但可以通过 this fiddle 证明.

问题似乎实际上发生在 knockout-2.1.0.js 的以下 block 中:

function fixUpVirtualElements(contiguousNodeArray) {
    // Ensures that contiguousNodeArray really *is* an array of contiguous siblings, even if some of the interior
    // ones have changed since your array was first built (e.g., because your array contains virtual elements, and
    // their virtual children changed when binding was applied to them).
    // This is needed so that we can reliably remove or update the nodes corresponding to a given array item

    if (contiguousNodeArray.length > 2) {
        // Build up the actual new contiguous node set
        var current = contiguousNodeArray[0], last = contiguousNodeArray[contiguousNodeArray.length - 1], newContiguousSet = [current];
        while (current !== last) {
            current = current.nextSibling;
            if (!current) // Won't happen, except if the developer has manually removed some DOM elements (then we're in an undefined scenario)
                return;
            newContiguousSet.push(current);
        }

        // ... then mutate the input array to match this.
        // (The following line replaces the contents of contiguousNodeArray with newContiguousSet)
        Array.prototype.splice.apply(contiguousNodeArray, [0, contiguousNodeArray.length].concat(newContiguousSet));
    }
}

此调用将移动的 DOM 元素添加到要删除的元素列表中,当移动的元素被移除时。

因此,向任何 jQuery UI/Knockoutjs 天才公开征求意见 - 是否有解决此冲突的方法,或者我是否需要做一些完全不同的事情才能使这些工具很好地协同工作?

最佳答案

我认为“最佳”解决方案是从 DOM 中删除该元素并更改其在 KO 中的位置。您可以在 sortable 的 stop 事件中执行此操作。 http://jsfiddle.net/vgrTY/4/

我继续将您的 array-contents 文本更改为计算文本,以便正确显示。

关于javascript - KnockoutJS/jQuery UI 可排序冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080839/

相关文章:

javascript - Google Maps API 仅显示一个信息窗口 - 循环问题

javascript - 使用指令将 jQueryUI 主题应用到带有 Angularjs 的按钮时出现 TypeError

javascript - $.ui 在自动完成和 ui 插件中未定义

javascript - 如何随机移动假光标?

javascript - jQuery UI 可拖动事件不起作用

jquery - 添加其中包含 javascript 的用户控件会破坏 jquery-ui

jquery - 当我删除对象时,jQuery 是否会自动从对象中删除附加的偶数处理程序?

javascript - 为什么我无法使用 GetElementById 查找页面上的元素?

javascript - 使用 JavaScript 展平 DOM 中的嵌套跨度以优化 HTML 编辑器输出

javascript - Mozilla 扩展的内容安全策略