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

Commit 428afa9

Browse files
committed
Merge pull request #444 from thgreasi/michaschwab-waitForNotDisabled0.14
feat(sortable): postpone sortable init when initially disabled
2 parents 5d540af + cbd3efd commit 428afa9

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

src/sortable.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ angular.module('ui.sortable', [])
230230
return;
231231
}
232232

233-
if (ngModel) {
234-
233+
function wireUp () {
235234
// When we add or remove elements, we need the sortable to 'refresh'
236235
// so it can find the new/removed elements.
237236
scope.$watchCollection('ngModel', function() {
@@ -443,13 +442,32 @@ angular.module('ui.sortable', [])
443442
}, true);
444443

445444
patchUISortableOptions(opts);
445+
}
446+
447+
function watchOnce (watchExpr, fn) {
448+
var cancelWatcher = scope.$watch(watchExpr, function () {
449+
fn.apply(this, arguments);
450+
cancelWatcher();
451+
});
452+
}
446453

447-
} else {
448-
$log.info('ui.sortable: ngModel not provided!', element);
454+
function lazyInit () {
455+
if (scope.uiSortable && scope.uiSortable.disabled) {
456+
watchOnce('uiSortable.disabled', lazyInit);
457+
} else {
458+
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+
}
449468
}
450469

451-
// Create sortable
452-
element.sortable(opts);
470+
lazyInit();
453471
}
454472
};
455473
}

test/sortable.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,67 @@ describe('uiSortable', function() {
302302

303303
});
304304

305+
it('should not initialize a disabled sortable', function() {
306+
inject(function($compile, $rootScope) {
307+
var element;
308+
var childScope = $rootScope.$new();
309+
spyOn(angular.element.fn, 'sortable');
310+
311+
childScope.items = ['One', 'Two', 'Three'];
312+
childScope.opts = {
313+
disabled: true
314+
};
315+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul>')(childScope);
316+
317+
expect(angular.element.fn.sortable).not.toHaveBeenCalled();
318+
});
319+
});
320+
321+
it('should lazily initialize a latelly enabled sortable (set disabled = false)', function() {
322+
inject(function($compile, $rootScope) {
323+
var element;
324+
var childScope = $rootScope.$new();
325+
spyOn(angular.element.fn, 'sortable');
326+
327+
childScope.items = ['One', 'Two', 'Three'];
328+
childScope.opts = {
329+
disabled: true
330+
};
331+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul>')(childScope);
332+
333+
expect(angular.element.fn.sortable).not.toHaveBeenCalled();
334+
335+
$rootScope.$apply(function() {
336+
childScope.opts.disabled = false;
337+
});
338+
339+
expect(angular.element.fn.sortable).toHaveBeenCalled();
340+
});
341+
});
342+
343+
it('should lazily initialize a latelly enabled sortable (delete disabled option)', function() {
344+
inject(function($compile, $rootScope) {
345+
var element;
346+
var childScope = $rootScope.$new();
347+
spyOn(angular.element.fn, 'sortable');
348+
349+
childScope.items = ['One', 'Two', 'Three'];
350+
childScope.opts = {
351+
disabled: true
352+
};
353+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul>')(childScope);
354+
355+
expect(angular.element.fn.sortable).not.toHaveBeenCalled();
356+
357+
$rootScope.$apply(function() {
358+
childScope.opts = {};
359+
});
360+
361+
expect(angular.element.fn.sortable).toHaveBeenCalled();
362+
});
363+
});
364+
365+
305366
});
306367

307368

0 commit comments

Comments
 (0)