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

Commit a723311

Browse files
committed
Merge pull request #315 from thgreasi/dataAnotationRefresh
fix(sortable): fix 'data-ui-sortable' directive annotation error
2 parents 11a3fd2 + 6db6808 commit a723311

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/sortable.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ angular.module('ui.sortable', [])
7676
$timeout(function() {
7777
// ensure that the jquery-ui-sortable widget instance
7878
// is still bound to the directive's element
79-
if (!!element.data('ui-sortable')) {
79+
if (!!element.sortable('instance')) {
8080
element.sortable('refresh');
8181
}
8282
}, 0, false);
@@ -87,7 +87,7 @@ angular.module('ui.sortable', [])
8787
// since the drag has started, the element will be
8888
// absolutely positioned, so we check its siblings
8989
var siblings = ui.item.siblings();
90-
angular.element(e.target).data('ui-sortable').floating = isFloating(siblings);
90+
angular.element(e.target).sortable('instance').floating = isFloating(siblings);
9191
}
9292

9393
// Save the starting position of dragged item
@@ -263,13 +263,13 @@ angular.module('ui.sortable', [])
263263
scope.$watch('uiSortable', function(newVal /*, oldVal*/) {
264264
// ensure that the jquery-ui-sortable widget instance
265265
// is still bound to the directive's element
266-
if (!!element.data('ui-sortable')) {
266+
if (!!element.sortable('instance')) {
267267
angular.forEach(newVal, function(value, key) {
268268
// if it's a custom option of the directive,
269269
// handle it approprietly
270270
if (key in directiveOpts) {
271271
if (key === 'ui-floating' && (value === false || value === true)) {
272-
element.data('ui-sortable').floating = value;
272+
element.sortable('instance').floating = value;
273273
}
274274

275275
opts[key] = value;

test/sortable.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ describe('uiSortable', function() {
7777
});
7878
});
7979

80+
it('should refresh sortable properly after an apply [data-* anotation]', function() {
81+
inject(function($compile, $rootScope, $timeout) {
82+
var element;
83+
var childScope = $rootScope.$new();
84+
element = $compile('<ul data-ui-sortable="opts" data-ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul>')(childScope);
85+
$rootScope.$apply(function() {
86+
childScope.items = ['One', 'Two', 'Three'];
87+
childScope.opts = {};
88+
});
89+
90+
expect(function() {
91+
$timeout.flush();
92+
}).not.toThrow();
93+
94+
expect(childScope.items).toEqual(['One', 'Two', 'Three']);
95+
expect(childScope.items).toEqual(listContent(element));
96+
});
97+
});
98+
8099
it('should not refresh sortable if destroyed', function() {
81100
inject(function($compile, $rootScope, $timeout) {
82101
var element;
@@ -94,6 +113,24 @@ describe('uiSortable', function() {
94113
});
95114
});
96115

116+
it('should not refresh sortable if destroyed [data-* anotation]', function() {
117+
inject(function($compile, $rootScope, $timeout) {
118+
var element;
119+
var childScope = $rootScope.$new();
120+
element = $compile('<div><ul data-ui-sortable="opts" data-ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul></div>')(childScope);
121+
$rootScope.$apply(function() {
122+
childScope.items = ['One', 'Two', 'Three'];
123+
childScope.opts = {};
124+
});
125+
126+
element.remove(element.firstChild);
127+
expect(function() {
128+
$timeout.flush();
129+
}).not.toThrow();
130+
131+
});
132+
});
133+
97134
it('should not try to apply options to a destroyed sortable', function() {
98135
inject(function($compile, $rootScope, $timeout) {
99136
var element;
@@ -115,6 +152,27 @@ describe('uiSortable', function() {
115152
});
116153
});
117154

155+
it('should not try to apply options to a destroyed sortable [data-* anotation]', function() {
156+
inject(function($compile, $rootScope, $timeout) {
157+
var element;
158+
var childScope = $rootScope.$new();
159+
element = $compile('<div><ul data-ui-sortable="opts" data-ng-model="items"><li ng-repeat="item in items">{{ item }}</li></ul></div>')(childScope);
160+
$rootScope.$apply(function() {
161+
childScope.items = ['One', 'Two', 'Three'];
162+
childScope.opts = {
163+
update: function() {}
164+
};
165+
166+
element.remove(element.firstChild);
167+
});
168+
169+
expect(function() {
170+
$timeout.flush();
171+
}).not.toThrow();
172+
173+
});
174+
});
175+
118176
});
119177

120178
});

0 commit comments

Comments
 (0)