From a479eae115afc39d3ffc803cccb74f3a7a61007a Mon Sep 17 00:00:00 2001 From: c4605 Date: Fri, 17 Apr 2015 16:08:47 +0800 Subject: [PATCH 1/2] clean watcher after element destroy --- ng-sortable.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ng-sortable.js b/ng-sortable.js index d340a59..09055b4 100644 --- a/ng-sortable.js +++ b/ng-sortable.js @@ -69,6 +69,7 @@ ngSortable = attrs.ngSortable, options = scope.$eval(ngSortable) || {}, source = getSource(el), + watchers = [], sortable ; @@ -154,7 +155,11 @@ })); $el.on('$destroy', function () { + angular.forEach(watchers, function (/** Function */unwatch) { + unwatch(); + }); sortable.destroy(); + watchers = null; sortable = null; nextSibling = null; }); @@ -164,7 +169,7 @@ 'sort', 'disabled', 'draggable', 'handle', 'animation', 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort' ], function (name) { - scope.$watch(ngSortable + '.' + name, function (value) { + watchers.push(scope.$watch(ngSortable + '.' + name, function (value) { if (value !== void 0) { options[name] = value; @@ -172,7 +177,7 @@ sortable.option(name, value); } } - }); + })); }); } } From 1ab661f10e69b2e67724f0796a3cd961d7dfacfc Mon Sep 17 00:00:00 2001 From: ha-D Date: Fri, 24 Apr 2015 12:31:34 +0430 Subject: [PATCH 2/2] Use isolated scope with two-way binding in ng-sortable --- ng-sortable.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/ng-sortable.js b/ng-sortable.js index 09055b4..c292791 100644 --- a/ng-sortable.js +++ b/ng-sortable.js @@ -64,10 +64,10 @@ // Export return { restrict: 'AC', + scope: { ngSortable: "=?" }, link: function (scope, $el, attrs) { var el = $el[0], - ngSortable = attrs.ngSortable, - options = scope.$eval(ngSortable) || {}, + options = scope.ngSortable || {}, source = getSource(el), watchers = [], sortable @@ -164,22 +164,20 @@ nextSibling = null; }); - if (ngSortable && !/{|}/.test(ngSortable)) { // todo: ugly - angular.forEach([ - 'sort', 'disabled', 'draggable', 'handle', 'animation', - 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort' - ], function (name) { - watchers.push(scope.$watch(ngSortable + '.' + name, function (value) { - if (value !== void 0) { - options[name] = value; - - if (!/^on[A-Z]/.test(name)) { - sortable.option(name, value); - } + angular.forEach([ + 'sort', 'disabled', 'draggable', 'handle', 'animation', + 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort' + ], function (name) { + watchers.push(scope.$watch('ngSortable.' + name, function (value) { + if (value !== void 0) { + options[name] = value; + + if (!/^on[A-Z]/.test(name)) { + sortable.option(name, value); } - })); - }); - } + } + })); + }); } }; }]);