Skip to content

Commit 4883224

Browse files
committed
fix(perf): less extra watcher per draggable/droppable if data-drag/data-drop is an interpolated string
Closes #168
1 parent 59e5889 commit 4883224

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/angular-dragdrop.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
257257
require: '?jqyouiDroppable',
258258
restrict: 'A',
259259
link: function(scope, element, attrs) {
260-
var dragSettings, jqyouiOptions, zIndex;
260+
var dragSettings, jqyouiOptions, zIndex, killWatcher;
261261
var updateDraggable = function(newValue, oldValue) {
262262
if (newValue) {
263263
dragSettings = scope.$eval(element.attr('jqyoui-draggable') || element.attr('data-jqyoui-draggable')) || {};
@@ -284,8 +284,14 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
284284
} else {
285285
element.draggable({disabled: true});
286286
}
287+
288+
if (killWatcher && angular.isDefined(newValue) && (angular.equals(attrs.drag, 'true') || angular.equals(attrs.drag, 'false'))) {
289+
killWatcher();
290+
killWatcher = null;
291+
}
287292
};
288-
scope.$watch(function() { return scope.$eval(attrs.drag); }, updateDraggable);
293+
294+
killWatcher = scope.$watch(function() { return scope.$eval(attrs.drag); }, updateDraggable);
289295
updateDraggable();
290296

291297
element.on('$destroy', function() {
@@ -298,7 +304,7 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
298304
restrict: 'A',
299305
priority: 1,
300306
link: function(scope, element, attrs) {
301-
var dropSettings;
307+
var dropSettings, killWatcher;
302308
var updateDroppable = function(newValue, oldValue) {
303309
if (newValue) {
304310
dropSettings = scope.$eval(angular.element(element).attr('jqyoui-droppable') || angular.element(element).attr('data-jqyoui-droppable')) || {};
@@ -340,11 +346,16 @@ var jqyoui = angular.module('ngDragDrop', []).service('ngDragDropService', ['$ti
340346
} else {
341347
element.droppable({disabled: true});
342348
}
349+
350+
if (killWatcher && angular.isDefined(newValue) && (angular.equals(attrs.drop, 'true') || angular.equals(attrs.drop, 'false'))) {
351+
killWatcher();
352+
killWatcher = null;
353+
}
343354
};
344355

345-
scope.$watch(function() { return scope.$eval(attrs.drop); }, updateDroppable);
356+
killWatcher = scope.$watch(function() { return scope.$eval(attrs.drop); }, updateDroppable);
346357
updateDroppable();
347-
358+
348359
element.on('$destroy', function() {
349360
element.droppable({disabled: true}).droppable('destroy');
350361
});

0 commit comments

Comments
 (0)