Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit cf47253

Browse files
committed
refactor(sortable): refactor lazy init code
1 parent 99306dd commit cf47253

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/sortable.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ angular.module('ui.sortable', [])
230230
return;
231231
}
232232

233-
function init () {
233+
function wireUp () {
234234
// When we add or remove elements, we need the sortable to 'refresh'
235235
// so it can find the new/removed elements.
236236
scope.$watchCollection('ngModel', function() {
@@ -444,31 +444,30 @@ angular.module('ui.sortable', [])
444444
patchUISortableOptions(opts);
445445
}
446446

447-
var stopDisabledWatcher = angular.noop;
447+
function watchOnce (watchExpr, fn) {
448+
var cancelWatcher = scope.$watch(watchExpr, function () {
449+
fn.apply(this, arguments);
450+
cancelWatcher();
451+
});
452+
}
448453

449-
var startIfEnabled = function () {
454+
function lazyInit () {
450455
if (scope.uiSortable && scope.uiSortable.disabled) {
451-
return false;
452-
}
453-
454-
if (ngModel) {
455-
init();
456+
watchOnce('uiSortable.disabled', lazyInit);
456457
} else {
457-
$log.info('ui.sortable: ngModel not provided!', element);
458-
}
459-
460-
// Create sortable
461-
element.sortable(opts);
462-
463-
// Stop Watcher
464-
stopDisabledWatcher();
465458

466-
return true;
467-
};
468-
469-
if (!startIfEnabled()) {
470-
stopDisabledWatcher = scope.$watch('uiSortable.disabled', startIfEnabled);
459+
if (ngModel) {
460+
wireUp();
461+
} else {
462+
$log.info('ui.sortable: ngModel not provided!', element);
463+
}
464+
465+
// Create sortable
466+
element.sortable(opts);
467+
}
471468
}
469+
470+
lazyInit();
472471
}
473472
};
474473
}

0 commit comments

Comments
 (0)