Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(directive): event now accessible as $event
Browse files Browse the repository at this point in the history
Closes 259
  • Loading branch information
mhevery committed Feb 22, 2012
1 parent 6216dc0 commit 1752c8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
34 changes: 18 additions & 16 deletions src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ var ngBindAttrDirective = ['$interpolate', function($interpolate) {
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* click.
* click. (Event object is available as `$event`)
*
* @example
<doc:example>
Expand All @@ -399,20 +399,22 @@ var ngBindAttrDirective = ['$interpolate', function($interpolate) {
* expressions and are compiled and executed within the current scope.
*
* Events that are handled via these handler are always configured not to propagate further.
*
* TODO: maybe we should consider allowing users to control event propagation in the future.
*/
var ngEventDirectives = {};
forEach(
'click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave'.split(' '),
function(name) {
var directiveName = directiveNormalize('ng-' + name);
ngEventDirectives[directiveName] = valueFn(function(scope, element, attr) {
element.bind(lowercase(name), function(event) {
scope.$apply(attr[directiveName]);
event.stopPropagation();
});
});
ngEventDirectives[directiveName] = ['$parse', function($parse) {
return function(scope, element, attr) {
var fn = $parse(attr[directiveName]);
element.bind(lowercase(name), function(event) {
scope.$apply(function() {
fn(scope, {$event:event});
});
});
};
}];
}
);

Expand All @@ -425,7 +427,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* dblclick.
* dblclick. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -441,7 +443,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mousedown.
* mousedown. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -457,7 +459,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mouseup.
* mouseup. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -472,7 +474,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mouseover.
* mouseover. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -488,7 +490,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mouseenter.
* mouseenter. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -504,7 +506,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mouseleave.
* mouseleave. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand All @@ -520,7 +522,7 @@ forEach(
*
* @element ANY
* @param {expression} expression {@link guide/dev_guide.expressions Expression} to evaluate upon
* mousemove.
* mousemove. (Event object is available as `$event`)
*
* @example
* See {@link angular.module.ng.$compileProvider.directive.ng:click ng:click}
Expand Down
24 changes: 0 additions & 24 deletions test/BinderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,30 +438,6 @@ describe('Binder', function() {
assertChild(5, false);
}));

it('ItShouldDisplayErrorWhenActionIsSyntacticlyIncorrect', function() {
module(function($exceptionHandlerProvider){
$exceptionHandlerProvider.mode('log');
});
inject(function($rootScope, $exceptionHandler, $log, $compile) {
element = $compile(
'<div>' +
'<input type="button" ng:click="greeting=\'ABC\'"/>' +
'<input type="button" ng:click=":garbage:"/>' +
'</div>')($rootScope);
var first = jqLite(element.find('input')[0]);
var second = jqLite(element.find('input')[1]);
var errorLogs = $log.error.logs;

browserTrigger(first, 'click');
expect($rootScope.greeting).toBe('ABC');
expect(errorLogs).toEqual([]);

browserTrigger(second, 'click');
expect($exceptionHandler.errors[0]).
toMatchError(/Syntax Error: Token ':' not a primary expression/);
});
});

it('ItShouldSelectTheCorrectRadioBox', inject(function($rootScope, $compile) {
element = $compile(
'<div>' +
Expand Down
11 changes: 4 additions & 7 deletions test/directivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,12 @@ describe("directive", function() {
expect($rootScope.clicked).toEqual(true);
}));

it('should stop event propagation', inject(function($rootScope, $compile) {
element = $compile('<div ng:click="outer = true"><div ng:click="inner = true"></div></div>')($rootScope);
it('should pass event object', inject(function($rootScope, $compile) {
element = $compile('<div ng:click="event = $event"></div>')($rootScope);
$rootScope.$digest();
expect($rootScope.outer).not.toBeDefined();
expect($rootScope.inner).not.toBeDefined();

browserTrigger(element.find('div'), 'click');
expect($rootScope.outer).not.toBeDefined();
expect($rootScope.inner).toEqual(true);
browserTrigger(element, 'click');
expect($rootScope.event).toBeDefined();
}));
});

Expand Down

0 comments on commit 1752c8c

Please sign in to comment.