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. F
For the first issue, there is a slightly optimized way that you could do it. arg.sourceParent
is contained in the tasksByRoute
item that you want to remove. The remove
function can take a function to run against the items. So, you could write it like:
self.afterMoveCallback = function(arg) {
if (arg.sourceParent().length === 0) {
self.tasksByRoute.remove(function(route) {
return route.tasks === arg.sourceParent;
});
}
};
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:
//track an index on items in an observableArray
ko.observableArray.fn.indexed = function(prop) {
prop = prop || 'index';
//whenever the array changes, make one loop to update the index on each
this.subscribe(function(newValue) {
if (newValue) {
var item;
for (var i = 0, j = newValue.length; i < j; i++) {
item = newValue[i];
if (!ko.isObservable(item[prop])) {
item[prop] = ko.observable();
}
item[prop](i); //add 1 here if you don't want it to be zero based
}
}
});
//initialize the index
this.valueHasMutated();
return this;
};
in your case, you would use it like:
self.scheduledTasks = ko.observableArray([
new Task(8, 4, "Route 4", "Cust 8", 1),
new Task(9, 4, "Route 4", "Cust 9", 2),
new Task(10, 5, "Route 5", "Cust 10")
]).indexed("order");
Here is a sample updated with both of these changes: http://jsfiddle.net/rniemeyer/usVKQ/