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

Commit 7e82930

Browse files
committed
Merge pull request #323 from thgreasi/callbackContext
fix(sortable): propagare the context to the combined/wrapped callbacks
2 parents b5fb6fd + 3a8dff1 commit 7e82930

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

Diff for: src/sortable.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ angular.module('ui.sortable', [])
1919

2020
function combineCallbacks(first,second){
2121
if(second && (typeof second === 'function')) {
22-
return function(e, ui) {
23-
first(e, ui);
24-
second(e, ui);
22+
return function() {
23+
first.apply(this, arguments);
24+
second.apply(this, arguments);
2525
};
2626
}
2727
return first;
@@ -286,7 +286,7 @@ angular.module('ui.sortable', [])
286286
wrappers.helper = function (inner) {
287287
if (inner && typeof inner === 'function') {
288288
return function (e, item) {
289-
var innerResult = inner(e, item);
289+
var innerResult = inner.apply(this, arguments);
290290
item.sortable._isCustomHelperUsed = item !== innerResult;
291291
return innerResult;
292292
};

Diff for: test/sortable.e2e.callbacks.spec.js

+75
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,81 @@ describe('uiSortable', function() {
270270
});
271271
});
272272

273+
it('should call all callbacks with the proper context', function() {
274+
inject(function($compile, $rootScope) {
275+
var element, callbackContexts = {};
276+
$rootScope.$apply(function() {
277+
$rootScope.opts = {
278+
helper: function(e, item) {
279+
callbackContexts.helper = this;
280+
return item;
281+
},
282+
create: function() {
283+
callbackContexts.create = this;
284+
},
285+
start: function() {
286+
callbackContexts.start = this;
287+
},
288+
activate: function() {
289+
callbackContexts.activate = this;
290+
},
291+
beforeStop: function() {
292+
callbackContexts.beforeStop = this;
293+
},
294+
update: function() {
295+
callbackContexts.update = this;
296+
},
297+
deactivate: function() {
298+
callbackContexts.deactivate = this;
299+
},
300+
stop: function() {
301+
callbackContexts.stop = this;
302+
}
303+
};
304+
spyOn($rootScope.opts, 'helper').andCallThrough();
305+
spyOn($rootScope.opts, 'create').andCallThrough();
306+
spyOn($rootScope.opts, 'start').andCallThrough();
307+
spyOn($rootScope.opts, 'activate').andCallThrough();
308+
spyOn($rootScope.opts, 'beforeStop').andCallThrough();
309+
spyOn($rootScope.opts, 'update').andCallThrough();
310+
spyOn($rootScope.opts, 'deactivate').andCallThrough();
311+
spyOn($rootScope.opts, 'stop').andCallThrough();
312+
$rootScope.items = ['One', 'Two', 'Three'];
313+
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
314+
});
315+
316+
host.append(element);
317+
318+
$rootScope.$apply(function() {
319+
});
320+
var li = element.find(':eq(0)');
321+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
322+
li.simulate('drag', { dy: dy });
323+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
324+
expect($rootScope.items).toEqual(listContent(element));
325+
326+
expect($rootScope.opts.helper).toHaveBeenCalled();
327+
expect($rootScope.opts.create).toHaveBeenCalled();
328+
expect($rootScope.opts.start).toHaveBeenCalled();
329+
expect($rootScope.opts.activate).toHaveBeenCalled();
330+
expect($rootScope.opts.beforeStop).toHaveBeenCalled();
331+
expect($rootScope.opts.update).toHaveBeenCalled();
332+
expect($rootScope.opts.deactivate).toHaveBeenCalled();
333+
expect($rootScope.opts.stop).toHaveBeenCalled();
334+
335+
expect(callbackContexts.helper).toEqual(element[0]);
336+
expect(callbackContexts.create).toEqual(element[0]);
337+
expect(callbackContexts.start).toEqual(element[0]);
338+
expect(callbackContexts.activate).toEqual(element[0]);
339+
expect(callbackContexts.beforeStop).toEqual(element[0]);
340+
expect(callbackContexts.update).toEqual(element[0]);
341+
expect(callbackContexts.deactivate).toEqual(element[0]);
342+
expect(callbackContexts.stop).toEqual(element[0]);
343+
344+
$(element).remove();
345+
});
346+
});
347+
273348
it('should properly free ui.item.sortable object', function() {
274349
inject(function($compile, $rootScope) {
275350
var element, uiItem, uiItemSortable_Destroy;

0 commit comments

Comments
 (0)