Skip to content

Instantly share code, notes, and snippets.

@fercreek
Forked from toddmotto/listeners.js
Created June 7, 2016 15:52
Show Gist options
  • Select an option

  • Save fercreek/3d46ff2bccd1ca42fc5c4af23dd0ed35 to your computer and use it in GitHub Desktop.

Select an option

Save fercreek/3d46ff2bccd1ca42fc5c4af23dd0ed35 to your computer and use it in GitHub Desktop.
Automatic unbinding on $scope.$destroy for $rootScope listeners
/*!
* $rootScope listeners, remember to unbind on $destroy
*/
var $rootListeners = {
'transmitProgress': $rootScope.$on('transmit:progress', transmitProgress),
'transmitSuccess': $rootScope.$on('transmit:success', transmitSuccess),
'transmitError': $rootScope.$on('transmit:error', transmitError)
};
// iterate the Object and pass the methods to be called on $destroy
for (var unbind in $rootListeners) {
$scope.$on('$destroy', $rootListeners[unbind]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment