I'm playing with angularjs doing some simple stuff. I wanted to incorporate some real time messaging like PubNub, PubSub or Pusher. Basically I have it working, but it seems too cumbersome in some areas. Maybe you can show me a better way.
Ok, so here is my working jsfiddle (with pubsub faked) http://jsfiddle.net/canree/dD5VR/
I have external service (pubsub
) and created angular service for it.
Now as I don't want other parts of my app to be dependent on this concrete implementation , I decided to create another one (inbox
) that would store all the messages pubsub
sent. And this inbox
would be the dependency across my app (instead of pubsub
).
So I want my dependencies to be:
controller -> inbox -> pubsub
When pubsub
receives a message it needs to add it to inbox
so that controller can see it and react accordingly. The problem is I don't want this backward dependency (pubsub -> inbox
). I'd like pubsub
to notify observers that message was received.
What I have now is ugly callback registration (inbox registers callback in pubsub). I don't like it very much as it has a lot of boilerplate (regarding this
handling etc).
I wonder if there is other way to connect those services? Events (but without being forced to use $rootScope
everywhere)? If so, where to register such event handler? Can services receive events?
Take a look at the fiddle provided and advice me how to solve it better way.