mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-26 11:10:25 +00:00
feat(scope): scope.$emit/$broadcast return the event object, add cancelled property
This commit is contained in:
parent
eb92735c9e
commit
6e635012fb
2 changed files with 28 additions and 3 deletions
|
|
@ -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;
|
||||
|
|
@ -601,7 +601,7 @@ 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);
|
||||
}
|
||||
|
|
@ -609,6 +609,8 @@ function $RootScopeProvider(){
|
|||
//traverse upwards
|
||||
scope = scope.$parent;
|
||||
} while (scope);
|
||||
|
||||
return event;
|
||||
},
|
||||
|
||||
|
||||
|
|
@ -662,6 +664,8 @@ function $RootScopeProvider(){
|
|||
}
|
||||
}
|
||||
} while ((current = next));
|
||||
|
||||
return event;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue