Is there a way to extend an object returned by an Angular Service, Factory, or Provider with event pub/sub?
I'd like to do something like:
angular.module('app').factory('user', function(Event){
var user = {
// User methods...
};
return angular.extend(user, Event);
})
// In a distant time and place...
angular.module('app').directive('nav', function(user){
scope: true,
templateUrl: 'something',
link: function(scope){
user.on('logIn', function(user){
scope.user = user
})
}
})
I accomplished something similar with promises. That is usually a good way to communicate asynchronously out of a service, but a user can log out and in, and promises only resolve once. Events are a better fit here.
I also understand I could solve this by broadcasting/emiting on $rootScope
and listening on scope
, but I think that is smelly. :)
What kinds of things patterns have you guys come up with to mediate between services?
I'm use (a modified version of) a pubsub service that you can check out here.
You can read about the trade offs for $rootScope events and pubsub with this question
Hope this helped