Question

Hi I'm building an angularjs app that has a timer throughout the application. Every minute I will be polling the server to see if a user has gotten a new message in their inbox.

I was wondering what is the best way to design this? I see two options so far:

1.) Rootscope function 2.) A Service

The problem with the service approach is that I have to inject this service to every controller I have. Does anybody else have any suggestions on how to design this app?

Thanks

Was it helpful?

Solution

Use a service - it's a cross-cutting concern that some, but probably not all of your controllers need to know about. Injecting it where it is needed does involve some extra keystrokes at first, but it's a benefit, not a problem - it keeps your code modular and easier to test.

If you use a service, you can inject a stubbed instance into your controller tests so you can test the controllers in isolation. You can use Jasmine spies (for example) to return mock values from your service, so you can easily test how your controllers behave with a range of inputs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top