From e0eb74da6a9c454d5bf5fe270a8a9ba5aeb66c49 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 22 Jan 2015 00:08:43 +0200 Subject: [PATCH 1/2] test(sortable): add test for `this` inside the option callbacks --- test/sortable.e2e.callbacks.spec.js | 75 +++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/sortable.e2e.callbacks.spec.js b/test/sortable.e2e.callbacks.spec.js index ae4ceb5..37825b9 100644 --- a/test/sortable.e2e.callbacks.spec.js +++ b/test/sortable.e2e.callbacks.spec.js @@ -270,6 +270,81 @@ describe('uiSortable', function() { }); }); + it('should call all callbacks with the proper context', function() { + inject(function($compile, $rootScope) { + var element, callbackContexts = {}; + $rootScope.$apply(function() { + $rootScope.opts = { + helper: function(e, item) { + callbackContexts.helper = this; + return item; + }, + create: function() { + callbackContexts.create = this; + }, + start: function() { + callbackContexts.start = this; + }, + activate: function() { + callbackContexts.activate = this; + }, + beforeStop: function() { + callbackContexts.beforeStop = this; + }, + update: function() { + callbackContexts.update = this; + }, + deactivate: function() { + callbackContexts.deactivate = this; + }, + stop: function() { + callbackContexts.stop = this; + } + }; + spyOn($rootScope.opts, 'helper').andCallThrough(); + spyOn($rootScope.opts, 'create').andCallThrough(); + spyOn($rootScope.opts, 'start').andCallThrough(); + spyOn($rootScope.opts, 'activate').andCallThrough(); + spyOn($rootScope.opts, 'beforeStop').andCallThrough(); + spyOn($rootScope.opts, 'update').andCallThrough(); + spyOn($rootScope.opts, 'deactivate').andCallThrough(); + spyOn($rootScope.opts, 'stop').andCallThrough(); + $rootScope.items = ['One', 'Two', 'Three']; + element = $compile('')($rootScope); + }); + + host.append(element); + + $rootScope.$apply(function() { + }); + var li = element.find(':eq(0)'); + var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight(); + li.simulate('drag', { dy: dy }); + expect($rootScope.items).toEqual(['Two', 'Three', 'One']); + expect($rootScope.items).toEqual(listContent(element)); + + expect($rootScope.opts.helper).toHaveBeenCalled(); + expect($rootScope.opts.create).toHaveBeenCalled(); + expect($rootScope.opts.start).toHaveBeenCalled(); + expect($rootScope.opts.activate).toHaveBeenCalled(); + expect($rootScope.opts.beforeStop).toHaveBeenCalled(); + expect($rootScope.opts.update).toHaveBeenCalled(); + expect($rootScope.opts.deactivate).toHaveBeenCalled(); + expect($rootScope.opts.stop).toHaveBeenCalled(); + + expect(callbackContexts.helper).toEqual(element[0]); + expect(callbackContexts.create).toEqual(element[0]); + expect(callbackContexts.start).toEqual(element[0]); + expect(callbackContexts.activate).toEqual(element[0]); + expect(callbackContexts.beforeStop).toEqual(element[0]); + expect(callbackContexts.update).toEqual(element[0]); + expect(callbackContexts.deactivate).toEqual(element[0]); + expect(callbackContexts.stop).toEqual(element[0]); + + $(element).remove(); + }); + }); + it('should properly free ui.item.sortable object', function() { inject(function($compile, $rootScope) { var element, uiItem, uiItemSortable_Destroy; From 3a8dff154acf2149fd73578cf87da6db62f563f0 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 22 Jan 2015 00:10:18 +0200 Subject: [PATCH 2/2] fix(sortable): propagare the context to the combined/wrapped callbacks Closes #251 --- src/sortable.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sortable.js b/src/sortable.js index 85f1f74..14c2679 100644 --- a/src/sortable.js +++ b/src/sortable.js @@ -19,9 +19,9 @@ angular.module('ui.sortable', []) function combineCallbacks(first,second){ if(second && (typeof second === 'function')) { - return function(e, ui) { - first(e, ui); - second(e, ui); + return function() { + first.apply(this, arguments); + second.apply(this, arguments); }; } return first; @@ -286,7 +286,7 @@ angular.module('ui.sortable', []) wrappers.helper = function (inner) { if (inner && typeof inner === 'function') { return function (e, item) { - var innerResult = inner(e, item); + var innerResult = inner.apply(this, arguments); item.sortable._isCustomHelperUsed = item !== innerResult; return innerResult; };