Created
October 6, 2013 21:09
-
-
Save thebigredgeek/6859145 to your computer and use it in GitHub Desktop.
$rootScope candy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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