From 91db99208e197a73584a88a8d835eeb55c466335 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Fri, 27 Apr 2012 13:14:46 +0200 Subject: [PATCH 1/2] refactor(scope.$emit): rename event.cancel() to event.stopPropagation() Breaks event.cancel() is event.stopPropagation() --- src/ng/rootScope.js | 9 ++++----- test/ng/rootScopeSpec.js | 15 ++------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index a37eda5a8ee0..5bd652147162 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -616,9 +616,8 @@ function $RootScopeProvider(){ * - `targetScope` - {Scope}: the scope on which the event was `$emit`-ed or `$broadcast`-ed. * - `currentScope` - {Scope}: the current scope which is handling the event. * - `name` - {string}: Name of the event. - * - `cancel` - {function=}: calling `cancel` function will cancel further event propagation + * - `stopPropagation` - {function=}: calling `stopPropagation` function will cancel further event propagation * (available only for events that were `$emit`-ed). - * - `cancelled` - {boolean}: Whether the event was cancelled. */ $on: function(name, listener) { var namedListeners = this.$$listeners[name]; @@ -659,11 +658,11 @@ function $RootScopeProvider(){ var empty = [], namedListeners, scope = this, + stopPropagation = false, event = { name: name, targetScope: scope, - cancel: function() {event.cancelled = true;}, - cancelled: false + stopPropagation: function() {stopPropagation = true;} }, listenerArgs = concat([event], arguments, 1), i, length; @@ -674,7 +673,7 @@ function $RootScopeProvider(){ for (i=0, length=namedListeners.length; i1>'); }); @@ -685,17 +685,6 @@ describe('Scope', function() { }); - it('should return event object with cancelled property', function() { - child.$on('some', function(event) { - event.cancel(); - }); - - var result = grandChild.$emit('some'); - expect(result).toBeDefined(); - expect(result.cancelled).toBe(true); - }); - - describe('event object', function() { it('should have methods/properties', function() { var event; From 84542d2431d20de42d6ec27c9d3435dd72dbe2ee Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Fri, 27 Apr 2012 17:31:11 +0200 Subject: [PATCH 2/2] 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); + }); }); });