Permalink
Browse files

feat(scope): add event.preventDefault() and event.defaultPrevented

  • Loading branch information...
vojtajina committed Apr 27, 2012
1 parent 91db992 commit 84542d2431d20de42d6ec27c9d3435dd72dbe2ee
Showing with 27 additions and 3 deletions.
  1. +15 −3 src/ng/rootScope.js
  2. +12 −0 test/ng/rootScopeSpec.js
View
@@ -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];
@@ -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;
@@ -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
View
@@ -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);
});
});
});

0 comments on commit 84542d2

Please sign in to comment.