Skip to content

Commit

Permalink
fix(gestureDirectives): fix problem with event being passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jun 17, 2014
1 parent 405f372 commit b4b9407
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
30 changes: 13 additions & 17 deletions js/angular/directive/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
}];
}
36 changes: 36 additions & 0 deletions test/unit/angular/directive/gesture.unit.js
Original file line number Diff line number Diff line change
@@ -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('<div ' + directive.htmlName + '="foo(1, $event)">')($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);
}));
});
});
Empty file.

0 comments on commit b4b9407

Please sign in to comment.