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

Commit a6b9bd6

Browse files
committed
fix(sortable): race condition when DOM changes inside a digest loop.
1 parent 3b9ebf5 commit a6b9bd6

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.12.2",
3+
"version": "0.12.3",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.12.2",
3+
"version": "0.12.3",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

src/sortable.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,21 @@ angular.module('ui.sortable', [])
174174
};
175175

176176
scope.$watch(attrs.uiSortable, function(newVal /*, oldVal*/) {
177-
angular.forEach(newVal, function(value, key) {
178-
if(callbacks[key]) {
179-
if( key === 'stop' ){
180-
// call apply after stop
181-
value = combineCallbacks(
182-
value, function() { scope.$apply(); });
177+
if (!!element.data('ui-sortable')) {
178+
angular.forEach(newVal, function(value, key) {
179+
if(callbacks[key]) {
180+
if( key === 'stop' ){
181+
// call apply after stop
182+
value = combineCallbacks(
183+
value, function() { scope.$apply(); });
184+
}
185+
// wrap the callback
186+
value = combineCallbacks(callbacks[key], value);
183187
}
184-
// wrap the callback
185-
value = combineCallbacks(callbacks[key], value);
186-
}
187-
element.sortable('option', key, value);
188-
});
188+
189+
element.sortable('option', key, value);
190+
});
191+
}
189192
}, true);
190193

191194
angular.forEach(callbacks, function(value, key) {

test/sortable.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ describe('uiSortable', function() {
4646
});
4747
});
4848

49+
it('should not try to apply options to a destroyed sortable', function() {
50+
inject(function($compile, $rootScope, $timeout) {
51+
var element;
52+
var childScope = $rootScope.$new();
53+
element = $compile('<div><ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul></div>')(childScope);
54+
$rootScope.$apply(function() {
55+
childScope.items = ['One', 'Two', 'Three'];
56+
childScope.opts = {
57+
update: function() {}
58+
};
59+
60+
element.remove(element.firstChild);
61+
});
62+
63+
expect(function() {
64+
$timeout.flush();
65+
}).not.toThrow();
66+
67+
});
68+
});
69+
4970
});
5071

5172
});

0 commit comments

Comments
 (0)