From 44fc47ecf7beedfe5112e29be2815a1c226348fa Mon Sep 17 00:00:00 2001 From: Hauke Jaeger Date: Tue, 28 Jan 2014 11:45:24 +0100 Subject: [PATCH 1/2] move DOM nodes instead of removing and adding them again when moving an item within it's current list --- src/knockout-sortable.js | 49 ++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/knockout-sortable.js b/src/knockout-sortable.js index 1f33c14..7a5376e 100644 --- a/src/knockout-sortable.js +++ b/src/knockout-sortable.js @@ -203,21 +203,52 @@ if (targetIndex >= 0) { if (sourceParent) { - sourceParent.splice(sourceIndex, 1); + if (sourceParent !== targetParent) { + // moving from one list to another - //if using deferred updates plugin, force updates - if (ko.processAllDeferredBindingUpdates) { - ko.processAllDeferredBindingUpdates(); + sourceParent.splice(sourceIndex, 1); + + //if using deferred updates plugin, force updates + if (ko.processAllDeferredBindingUpdates) { + ko.processAllDeferredBindingUpdates(); + } + + targetParent.splice(targetIndex, 0, item); + + //rendering is handled by manipulating the observableArray; ignore dropped element + dataSet(el, ITEMKEY, null); + ui.item.remove(); + } + else { + // moving within same list + var underlyingList = unwrap(sourceParent); + + // notify 'beforeChange' subscribers + sourceParent.valueWillMutate(); + + underlyingList.splice(sourceIndex, 1); + + //if using deferred updates plugin, force updates + if (ko.processAllDeferredBindingUpdates) { + ko.processAllDeferredBindingUpdates(); + } + + underlyingList.splice(targetIndex, 0, item); + + // notify subscribers + sourceParent.valueHasMutated(); } } + else { + // drop new element from outside + targetParent.splice(targetIndex, 0, item); - targetParent.splice(targetIndex, 0, item); + //rendering is handled by manipulating the observableArray; ignore dropped element + dataSet(el, ITEMKEY, null); + ui.item.remove(); + } } - //rendering is handled by manipulating the observableArray; ignore dropped element - dataSet(el, ITEMKEY, null); - ui.item.remove(); - //if using deferred updates plugin, force updates if (ko.processAllDeferredBindingUpdates) { ko.processAllDeferredBindingUpdates(); From 8efab70c259dfbdeb3365505ecbb9dad43a01471 Mon Sep 17 00:00:00 2001 From: Hauke Jaeger Date: Thu, 20 Feb 2014 12:35:42 +0100 Subject: [PATCH 2/2] removed processAllDeferredBindingUpdates when moving items within their current list --- src/knockout-sortable.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/knockout-sortable.js b/src/knockout-sortable.js index 7a5376e..5a9b7c1 100644 --- a/src/knockout-sortable.js +++ b/src/knockout-sortable.js @@ -226,13 +226,9 @@ // notify 'beforeChange' subscribers sourceParent.valueWillMutate(); + // move from source index ... underlyingList.splice(sourceIndex, 1); - - //if using deferred updates plugin, force updates - if (ko.processAllDeferredBindingUpdates) { - ko.processAllDeferredBindingUpdates(); - } - + // ... to target index underlyingList.splice(targetIndex, 0, item); // notify subscribers