Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Created October 6, 2013 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebigredgeek/6859145 to your computer and use it in GitHub Desktop.
Save thebigredgeek/6859145 to your computer and use it in GitHub Desktop.
$rootScope candy
$rootScope.$safeApply = function (fn) { #safe apply can be accessed from any $scope
var phase;
phase = this.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn) {
fn();
}
} else {
this.$apply(fn);
}
return true;
};
$rootScope.$once = function (event, fn) { #once can be accessed from any scope
var killListener, self;
self = this;
killListener = self.$on(event, function (event, parameters) {
self.$safeApply(function () {
fn(event, parameters);
killListener();
return true;
});
return true;
});
return killListener;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment