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

Commit 84542d2

Browse files
committed
feat(scope): add event.preventDefault() and event.defaultPrevented
1 parent 91db992 commit 84542d2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/ng/rootScope.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ function $RootScopeProvider(){
618618
* - `name` - {string}: Name of the event.
619619
* - `stopPropagation` - {function=}: calling `stopPropagation` function will cancel further event propagation
620620
* (available only for events that were `$emit`-ed).
621+
* - `preventDefault` - {function}: calling `preventDefault` sets `defaultPrevented` flag to true.
622+
* - `defaultPrevented` - {boolean}: true if `preventDefault` was called.
621623
*/
622624
$on: function(name, listener) {
623625
var namedListeners = this.$$listeners[name];
@@ -662,7 +664,11 @@ function $RootScopeProvider(){
662664
event = {
663665
name: name,
664666
targetScope: scope,
665-
stopPropagation: function() {stopPropagation = true;}
667+
stopPropagation: function() {stopPropagation = true;},
668+
preventDefault: function() {
669+
event.defaultPrevented = true;
670+
},
671+
defaultPrevented: false
666672
},
667673
listenerArgs = concat([event], arguments, 1),
668674
i, length;
@@ -712,8 +718,14 @@ function $RootScopeProvider(){
712718
var target = this,
713719
current = target,
714720
next = target,
715-
event = { name: name,
716-
targetScope: target },
721+
event = {
722+
name: name,
723+
targetScope: target,
724+
preventDefault: function() {
725+
event.defaultPrevented = true;
726+
},
727+
defaultPrevented: false
728+
},
717729
listenerArgs = concat([event], arguments, 1);
718730

719731
//down while you can, then up and next sibling or up and next sibling until back at root

test/ng/rootScopeSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,18 @@ describe('Scope', function() {
697697
grandChild.$emit('myEvent');
698698
expect(event).toBeDefined();
699699
});
700+
701+
702+
it('should have preventDefault method and defaultPrevented property', function() {
703+
var event = grandChild.$emit('myEvent');
704+
expect(event.defaultPrevented).toBe(false);
705+
706+
child.$on('myEvent', function(event) {
707+
event.preventDefault();
708+
});
709+
event = grandChild.$emit('myEvent');
710+
expect(event.defaultPrevented).toBe(true);
711+
});
700712
});
701713
});
702714

0 commit comments

Comments
 (0)