2012-08-22 65 views
4

我试图使用KnockoutJS和jQuery UI排序在一起。我知道这已经做过(特别是,knockout-sortable),但我的用例有一些非常具体的行为,我希望避免尝试进行切换。KnockoutJS/jQuery UI可排序冲突

无论如何,这个问题是非常简单的 - 与jQuery UI可排序移动DOM元素后,敲除去除绑定到该DOM元素observableArray元件时行为异常。它将无法移除移动的元素,并且如果移入移动元素位置的元素被移除,它将移除该元素和原来移动的元素。很难说,但通过this fiddle证明。

似乎实际上发生在随后的块中敲除2.1.0.js问题:

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天才的公开调用 - 是否有办法解决这个冲突,还是我需要做一些完全不同的事情来让这些工具在一起很好地发挥作用?

回答

3

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

我继续前进,并将您的array-contents文本更改为计算以及它将正确显示。

+0

这工作完美 - 谢谢你! – Joel