Scope Decorator: $onRootScope
Decorates the $rootScope
with an $onRootScope
method as suggested here
app.config(['$provide', function($provide){
$provide.decorator('$rootScope', ['$delegate', function($delegate){
// [Object.defineProperty()][2]
// To make sure the $onRootScope property doesn't show up unexpected when enumerating over $scope we use `Object.defineProperty()` and set enumerable to false. Keep in mind that you might need an ES5 shim.
Object.defineProperty($delegate.constructor.prototype, '$onRootScope', {
value: function(name, listener){
var unsubscribe = $delegate.$on(name, listener);
this.$on('$destroy', unsubscribe);
},
enumerable: false
});
return $delegate;
}]);
}]);