From 84542d2431d20de42d6ec27c9d3435dd72dbe2ee Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Fri, 27 Apr 2012 17:31:11 +0200 Subject: [PATCH] feat(scope): add event.preventDefault() and event.defaultPrevented --- src/ng/rootScope.js | 18 +++++++++++++++--- test/ng/rootScopeSpec.js | 12 ++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 5bd652147162..bb12362b74f6 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -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 diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index 3faeb162054d..9d343b0848a8 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -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); + }); }); });