fix(gestureDirectives): fix problem with event being passed in

This commit is contained in:
Andrew Joslin
2014-06-17 13:13:35 +00:00
parent 405f372b66
commit b4b94073d5
3 changed files with 50 additions and 18 deletions

View File

@@ -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 gesture = $ionicGesture.on(eventType, listener, $element);
scope.$on('$destroy', function() {
$ionicGesture.off(gesture, eventType, listener);
var listener = function(ev) {
scope.$apply(function() {
fn(scope, {
$event: ev
});
};
}
});
};
var gesture = $ionicGesture.on(eventType, listener, element);
scope.$on('$destroy', function() {
$ionicGesture.off(gesture, eventType, listener);
});
};
}];
}

View File

@@ -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);
}));
});
});

View File