Hello all,
I've been loving working with Knockout.js. It's an incredible tool. Thank-you to everyone who has worked on it. :-)
However, I've been struggling with a problem when using jQuery UI's sortable, and I believe it might just be a bug in Knockout. I've reproduced a minimal testcase on jsfiddle:
http://jsfiddle.net/9EUym/. In this test scenario, a model is used to populate a 5 element <ul>. If you drag "Item #1" down and place it between "Item #3" and "Item #4", then magically a second, new, "Item #1" will appear. The problem seems to be triggered by reordering items in an observableArray after jQuery UI has been used to sort them; I'm doing this in a collaborative application to handle other-clients performing hand sorting operations reflecting in real-time on every user's display.
I've tried a few things to address this issue. I moved the modifications of the view model out of the sortable "stop" event handler, and/or used setTimeout to ensure that the sortable finishes working first. That had no effect. I used jQuery UI's "clone" helper option so that the original <li> item is not moved. That didn't have any effect. It seems that Knockout is losing track of the original DOM node and believes it has to create a new node. I assume that something in jQuery UI is triggering this behavior, but I've had difficulty reading and understanding the relevant portions of knockout to figure out how it maps DOM nodes to their data elements.
I would appreciate any advice as to how to proceed. I was thinking I'd disable parts of the jQuery UI sortable until I could maybe find what triggers this, but my gut feeling is that I'd just be hacking together a workaround where the problem lies elsewhere... any other approaches that might be a better idea?
Thanks,
Mathieu