Skip to content

Instantly share code, notes, and snippets.

@evanrs
Last active August 29, 2015 14:06
Show Gist options
  • Save evanrs/0e2fc6599e4df56364d6 to your computer and use it in GitHub Desktop.
Save evanrs/0e2fc6599e4df56364d6 to your computer and use it in GitHub Desktop.
Angular with granular Backbone style events.
ngModule.provider('$eventScope', function ($rootScopeProvider) {
var $get = $rootScopeProvider.$get.slice(-1)[0];
var $eventScope, proto, $cast, $emit;
this.$get = ['$injector', '$exceptionHandler', '$parse', '$browser',
function ($injector, $exceptionHandler, $parse, $browser) {
$eventScope = $eventScope || $get.apply($rootScopeProvider, arguments);
proto = proto || $eventScope.constructor.prototype;
$cast = $cast || proto.$broadcast;
$emit = $emit || proto.$emit;
proto.$emit = function(name, args) {
//(a:b:c)-> [] (a) => [a] (b) => [a, a:b] (c) => [a, a:b, a:b:c]
var event = $emit.call(this, name, args);
var cascade = name.split(':').slice(0,-1).join(':');
event.cascade = cascade && !event.stopCascade ?
this.$emit(cascade+'*', args) : false;
return event;
}
proto.$broadcast = function(name, args) {
//(a:b:c)-> [] (a) => [a] (b) => [a, a:b] (c) => [a, a:b, a:b:c]
var event = $cast.call(this, name, args);
var cascade = name.split(':').slice(0,-1).join(':');
event.cascade = cascade && !event.stopCascade ?
this.$broadcast(cascade+'*', args) : false;
return event;
}
return $eventScope;
}];
});
ngModule.run(function($rootScope) {
var proto = $rootScope.constructor.prototype;
var $emit = proto.$emit;
var $broadcast = proto.$broadcast;
proto.$emit = function(name, args) {
//(a:b:c)-> [] (a) => [a] (b) => [a, a:b] (c) => [a, a:b, a:b:c]
var event = $emit.call(this, name, args);
var cascade = name.split(':').slice(0,-1).join(':');
event.cascade = cascade && !event.stopCascade ?
this.$emit(cascade+'*', args) : false;
return event;
}
proto.$broadcast = function(name, args) {
//(a:b:c)-> [] (a) => [a] (b) => [a, a:b] (c) => [a, a:b, a:b:c]
var event = $broadcast.call(this, name, args);
var cascade = name.split(':').slice(0,-1).join(':');
event.cascade = cascade && !event.stopCascade ?
this.$broadcast(cascade+'*', args) : false;
return event;
}
});
ngModule.run(function($rootScope) {
var proto = $rootScope.constructor.prototype;
_.assign(proto, _.mapValues(_.pick(proto,'$emit', '$broadcast'),
function($fn, $fnKey) {
return function(name, args) {
//(a:b:c)-> [] (a) => [a] (b) => [a, a:b] (c) => [a, a:b, a:b:c]
var event = $fn.call(this, name, args)
var cascade = name.split(':').slice(0,-1).join(':');
event.cascade = cascade && !event.stopCascade ?
this[$fnKey](cascade+'*', args) : false;
return event;
}
}));
});
ngModule.run(function($rootScope) {
var $scope = $rootScope.$new();
$scope.$on("a:b:c", function(){
console.log(arguments[1], "\t==\t", "$scope.$on(a:b:c)");
});
$scope.$on("a:b*", function(){
console.log(arguments[1], "\t==\t", "$scope.$on(a:b*)");
});
$scope.$on("a*", function(){
console.log(arguments[1], "\t==\t", "$scope.$on(a*)");
});
$rootScope.$on("a:b:c", function() {
console.log(arguments[1], "\t==\t", "$rootScope.$on(a:b:c)");
});
$rootScope.$on("a:b*", function() {
console.log(arguments[1], "\t==\t", "$rootScope.$on(a:b*)");
});
$rootScope.$on("a*", function() {
console.log(arguments[1], "\t==\t", "$rootScope.$on(a*)");
});
$scope.$emit("a:b:c", "$scope.$emit(a:b:c)");
$scope.$broadcast("a:b:c", "$scope.$broadcast(a:b:c)");
$rootScope.$emit("a:b:c", "$rootScope.$emit(a:b:c)");
$rootScope.$broadcast("a:b:c", "$rootScope.$broadcast(a:b:c)");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment