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

Commit

Permalink
feat(scope): scope.$emit/$broadcast return the event object, add canc…
Browse files Browse the repository at this point in the history
…elled property
  • Loading branch information
vojtajina committed Feb 21, 2012
1 parent eb92735 commit 6e63501
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/service/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,12 @@ function $RootScopeProvider(){
$emit: function(name, args) {
var empty = [],
namedListeners,
canceled = false,
scope = this,
event = {
name: name,
targetScope: scope,
cancel: function() {canceled = true;}
cancel: function() {event.cancelled = true;},
cancelled: false
},
listenerArgs = concat([event], arguments, 1),
i, length;
Expand All @@ -601,14 +601,16 @@ function $RootScopeProvider(){
for (i=0, length=namedListeners.length; i<length; i++) {
try {
namedListeners[i].apply(null, listenerArgs);
if (canceled) return;
if (event.cancelled) return event;
} catch (e) {
$exceptionHandler(e);
}
}
//traverse upwards
scope = scope.$parent;
} while (scope);

return event;
},


Expand Down Expand Up @@ -662,6 +664,8 @@ function $RootScopeProvider(){
}
}
} while ((current = next));

return event;
}
};

Expand Down
21 changes: 21 additions & 0 deletions test/service/scopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,18 @@ describe('Scope', function() {
child.$emit('abc', 'arg1', 'arg2');
});


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 Down Expand Up @@ -752,6 +764,15 @@ describe('Scope', function() {
$rootScope.$broadcast('fooEvent');
expect(log).toBe('');
}));


it('should return event object', function() {
var result = child1.$broadcast('some');

expect(result).toBeDefined();
expect(result.name).toBe('some');
expect(result.targetScope).toBe(child1);
});
});


Expand Down

0 comments on commit 6e63501

Please sign in to comment.