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

Scope event changes #914

Merged
merged 2 commits into from
May 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ 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.
* - `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 @@ -659,11 +660,15 @@ 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;},
preventDefault: function() {
event.defaultPrevented = true;
},
defaultPrevented: false
},
listenerArgs = concat([event], arguments, 1),
i, length;
Expand All @@ -674,7 +679,7 @@ function $RootScopeProvider(){
for (i=0, length=namedListeners.length; i<length; i++) {
try {
namedListeners[i].apply(null, listenerArgs);
if (event.cancelled) return event;
if (stopPropagation) return event;
} catch (e) {
$exceptionHandler(e);
}
Expand Down Expand Up @@ -713,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
27 changes: 14 additions & 13 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ describe('Scope', function() {
}));


it('should allow cancelation of event propagation', function() {
child.$on('myEvent', function(event) { event.cancel(); });
it('should allow stopping event propagation', function() {
child.$on('myEvent', function(event) { event.stopPropagation(); });
grandChild.$emit('myEvent');
expect(log).toEqual('2>1>');
});
Expand All @@ -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;
Expand All @@ -708,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