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

Commit

Permalink
feat(scope): add event.preventDefault() and event.defaultPrevented
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed May 17, 2012
1 parent 91db992 commit 84542d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ function $RootScopeProvider(){
* - `name` - {string}: Name of the event.
* - `stopPropagation` - {function=}: calling `stopPropagation` function will cancel further event propagation
* (available only for events that were `$emit`-ed).
* - `preventDefault` - {function}: calling `preventDefault` sets `defaultPrevented` flag to true.
* - `defaultPrevented` - {boolean}: true if `preventDefault` was called.
*/
$on: function(name, listener) {
var namedListeners = this.$$listeners[name];
Expand Down Expand Up @@ -662,7 +664,11 @@ function $RootScopeProvider(){
event = {
name: name,
targetScope: scope,
stopPropagation: function() {stopPropagation = true;}
stopPropagation: function() {stopPropagation = true;},
preventDefault: function() {
event.defaultPrevented = true;
},
defaultPrevented: false
},
listenerArgs = concat([event], arguments, 1),
i, length;
Expand Down Expand Up @@ -712,8 +718,14 @@ function $RootScopeProvider(){
var target = this,
current = target,
next = target,
event = { name: name,
targetScope: target },
event = {
name: name,
targetScope: target,
preventDefault: function() {
event.defaultPrevented = true;
},
defaultPrevented: false
},
listenerArgs = concat([event], arguments, 1);

//down while you can, then up and next sibling or up and next sibling until back at root
Expand Down
12 changes: 12 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,18 @@ describe('Scope', function() {
grandChild.$emit('myEvent');
expect(event).toBeDefined();
});


it('should have preventDefault method and defaultPrevented property', function() {
var event = grandChild.$emit('myEvent');
expect(event.defaultPrevented).toBe(false);

child.$on('myEvent', function(event) {
event.preventDefault();
});
event = grandChild.$emit('myEvent');
expect(event.defaultPrevented).toBe(true);
});
});
});

Expand Down

0 comments on commit 84542d2

Please sign in to comment.