This is a follow up to a question: KnockoutJS Sortable group observableArray by field and conditionally sort
There are two further things I am battling to achieve. First when a nested list item is dragged to another list leaving the parent empty I would like to remove the parent. I have done this by creating an afterMove function and checking if the sourceParent length is 0. Then I look through the routes to see if any have an empty tasks array and remove accordingly. I would like to know if this is an efficient solution. I also have to remove the tasks otherwise the tasks subscription adds duplicates when removing scheduled tasks.
Second and most importantly the base object (Task) has an order property. I would like to bind this to the sortable order so that when the tasks are dragged around the Scheduled list the order property is updated. How do I access the order of other list items from the sortable callback?
Please see the following this Fiddle.
Any assistance would be greatly appreciated.
For the first issue, there is a slightly optimized way that you could do it.
arg.sourceParent
is contained in thetasksByRoute
item that you want to remove. Theremove
function can take a function to run against the items. So, you could write it like:For the second issue, I really like to use an extension that will automatically track the order in an observableArray. It would look something like:
in your case, you would use it like:
Here is a sample updated with both of these changes: http://jsfiddle.net/rniemeyer/usVKQ/