rniemeyer / knockout-sortable

A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality
MIT License
547 stars 128 forks source link

afterMove: Unexpected result as to which callback is executed #116

Open at-dro opened 10 years ago

at-dro commented 10 years ago

When using nested lists the executed callback is not always the expected one. Consider this fiddle: http://jsfiddle.net/gagczbrc/ When moving within one list or between two unrelated lists (e.g. moving Item 2.1 between 1.1. and 1.2) the callback is (as expected) called for the target list. When moving an item from a nested list to a parent list (e.g. Item 1.1 from List 1 to Root) also the target callback is executed. Unexpected behaviour occurs on moving from a parent list to a nested list (e.g. Item 2 from Root into List 1), because then the source callback (in this case alerting "Root") is called.

I think the reason for this lies in line 164. As noted there jQuery calls both callbacks for the source and target list (in this order). Normally the condition rules out the source list, because the (already moved) DOM item is no longer present within the source list. However it still is a descendant of the source list, when the target list is a descendant of the source list. Hence the wrong callback is executed.

I'm not sure if this can be fixed easily. Actually I consider it as a minor bug, because there is an easy workaround: Using one global callback in combination with a property of targetParent as explained here: http://stackoverflow.com/questions/21995159/knockoutjs-sortable-binding-how-to-get-targetparentnode

I think it should be mentioned in the documentation, though.