From b4b94073d5b26698be4a45b9fa4dbeb5a2cdcad4 Mon Sep 17 00:00:00 2001 From: Andrew Joslin Date: Tue, 17 Jun 2014 13:13:35 +0000 Subject: [PATCH] fix(gestureDirectives): fix problem with event being passed in --- js/angular/directive/gesture.js | 30 ++++++++--------- test/unit/angular/directive/gesture.unit.js | 36 +++++++++++++++++++++ test/unit/angular/directive/gestures.js | 0 3 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 test/unit/angular/directive/gesture.unit.js delete mode 100644 test/unit/angular/directive/gestures.js diff --git a/js/angular/directive/gesture.js b/js/angular/directive/gesture.js index 638fdb4be29..0e0e05c6f93 100644 --- a/js/angular/directive/gesture.js +++ b/js/angular/directive/gesture.js @@ -239,26 +239,22 @@ function gestureDirective(directiveName) { return ['$ionicGesture', '$parse', function($ionicGesture, $parse) { var eventType = directiveName.substr(2).toLowerCase(); - return { - restrict: 'A', - compile: function($element, attr) { - var fn = $parse( attr[directiveName] ); + return function(scope, element, attr) { + var fn = $parse( attr[directiveName] ); - return function(scope, element, attr) { - - var listener = function(ev) { - scope.$apply(function() { - fn(scope, {$event:event}); - }); - }; + var listener = function(ev) { + scope.$apply(function() { + fn(scope, { + $event: ev + }); + }); + }; - var gesture = $ionicGesture.on(eventType, listener, $element); + var gesture = $ionicGesture.on(eventType, listener, element); - scope.$on('$destroy', function() { - $ionicGesture.off(gesture, eventType, listener); - }); - }; - } + scope.$on('$destroy', function() { + $ionicGesture.off(gesture, eventType, listener); + }); }; }]; } diff --git a/test/unit/angular/directive/gesture.unit.js b/test/unit/angular/directive/gesture.unit.js new file mode 100644 index 00000000000..7d266dfb449 --- /dev/null +++ b/test/unit/angular/directive/gesture.unit.js @@ -0,0 +1,36 @@ +describe('gesture directive', function() { + beforeEach(module('ionic')); + + 'onHold onTap onTouch onRelease onDrag onDragUp onDragRight onDragDown onDragLeft onSwipe onSwipeUp onSwipeRight onSwipeBottom onSwipeLeft' + .split(' ') + .map(function(directiveName) { + return { + gestureName: directiveName.substr(2).toLowerCase(), + directiveName: directiveName, + htmlName: directiveName.replace(/[A-Z]/g, function(match, i) { + return '-' + match.toLowerCase(); + }) + }; + }) + .forEach(function(directive){ + it('should compile', inject(function($compile, $rootScope, $ionicGesture) { + var fakeGesture = {}; + spyOn($ionicGesture, 'on').andCallFake(function(eventType, listener, el) { + callback = listener; + return fakeGesture; + }); + spyOn($ionicGesture, 'off'); + var el = $compile('
')($rootScope.$new()); + $rootScope.$apply(); + + el.scope().foo = jasmine.createSpy('foo'); + + expect($ionicGesture.on.mostRecentCall.args[0]).toBe(directive.gestureName); + var event = {}; + callback(event); + expect(el.scope().foo).toHaveBeenCalledWith(1, event); + el.scope().$destroy(); + expect($ionicGesture.off).toHaveBeenCalledWith(fakeGesture, directive.gestureName, callback); + })); + }); +}); diff --git a/test/unit/angular/directive/gestures.js b/test/unit/angular/directive/gestures.js deleted file mode 100644 index e69de29bb2d..00000000000