Skip to content

Commit

Permalink
cancel sortable operation, so KO can take care of DOM manipulation, w…
Browse files Browse the repository at this point in the history
…hich ensures in KO 3+ that everything is disposed properly.
  • Loading branch information
rniemeyer committed Jun 16, 2014
1 parent 451880d commit 4f921f2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-sortable",
"version": "0.8.7",
"version": "0.8.8",
"main": "./build/knockout-sortable.min.js",
"ignore": [
"examples",
Expand Down
37 changes: 19 additions & 18 deletions build/knockout-sortable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-sortable 0.8.7 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-sortable 0.8.8 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -174,6 +174,7 @@
targetIndex = updateIndexFromDestroyedItems(targetIndex, targetParent);
}

//build up args for the callbacks
if (sortable.beforeMove || sortable.afterMove) {
arg = {
item: item,
Expand All @@ -184,31 +185,32 @@
targetIndex: targetIndex,
cancelDrop: false
};

//execute the configured callback prior to actually moving items
if (sortable.beforeMove) {
sortable.beforeMove.call(this, arg, event, ui);
}
}

if (sortable.beforeMove) {
sortable.beforeMove.call(this, arg, event, ui);
if (arg.cancelDrop) {
//call cancel on the correct list
if (arg.sourceParent) {
$(arg.sourceParent === arg.targetParent ? this : ui.sender).sortable("cancel");
}
//for a draggable item just remove the element
else {
$(el).remove();
}
//call cancel on the correct list, so KO can take care of DOM manipulation
if (sourceParent) {
$(sourceParent === targetParent ? this : ui.sender).sortable("cancel");
}
//for a draggable item just remove the element
else {
$(el).remove();
}

return;
}
//if beforeMove told us to cancel, then we are done
if (arg && arg.cancelDrop) {
return;
}

//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//in KO 3+, nodes outside of the original parent aren't found when trying to dispose, need to do this manually
ko.removeNode(el);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
Expand All @@ -220,7 +222,6 @@

//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) {
Expand Down
4 changes: 2 additions & 2 deletions build/knockout-sortable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-sortable",
"version": "0.8.7",
"version": "0.8.8",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand Down
35 changes: 18 additions & 17 deletions src/knockout-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
targetIndex = updateIndexFromDestroyedItems(targetIndex, targetParent);
}

//build up args for the callbacks
if (sortable.beforeMove || sortable.afterMove) {
arg = {
item: item,
Expand All @@ -183,31 +184,32 @@
targetIndex: targetIndex,
cancelDrop: false
};

//execute the configured callback prior to actually moving items
if (sortable.beforeMove) {
sortable.beforeMove.call(this, arg, event, ui);
}
}

if (sortable.beforeMove) {
sortable.beforeMove.call(this, arg, event, ui);
if (arg.cancelDrop) {
//call cancel on the correct list
if (arg.sourceParent) {
$(arg.sourceParent === arg.targetParent ? this : ui.sender).sortable("cancel");
}
//for a draggable item just remove the element
else {
$(el).remove();
}
//call cancel on the correct list, so KO can take care of DOM manipulation
if (sourceParent) {
$(sourceParent === targetParent ? this : ui.sender).sortable("cancel");
}
//for a draggable item just remove the element
else {
$(el).remove();
}

return;
}
//if beforeMove told us to cancel, then we are done
if (arg && arg.cancelDrop) {
return;
}

//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//in KO 3+, nodes outside of the original parent aren't found when trying to dispose, need to do this manually
ko.removeNode(el);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
Expand All @@ -219,7 +221,6 @@

//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) {
Expand Down

0 comments on commit 4f921f2

Please sign in to comment.