After noticing that I've added similar event listening code to many of my controllers, I began to think about how to streamline it. The code looks something like this:
document.addEventListener("resume", function(e){
$scope.doSomething();
}, false);
It became apparent that having the same code scattered throughout isn't ideal, especially when the only variation is the $scope.doSomething()
.
My idea was to create a directive so that I could use something like this:
<div on-resume="doSomething()">
</div>
I attempted to implement this (unsuccessfully):
.directive('onResume', function(){
return {
restrict: 'A',
link: function(scope, elem, attr, ctrl) {
elem.bind('resume', function(e) {
fnName = attributes["onResume"];
scope.$apply(function(){
scope[fnName]();
});
});
}
};
});
Any suggestions or insights?