1

I have a simple question about best practices.

A service loads once at load time.

Let's say there is a method called getUser in the service called user. I have to call getUser in several controllers.

The GET request will happen twice right? Is there a good practice to check whether the data has already been fetched to avoid this second call?

jamiltz
  • 1,144
  • 8
  • 15
  • I wondered the same about here is some helpful info [Angular Best Practices](http://stackoverflow.com/q/20802798/1959948) – Dalorzo May 17 '14 at 22:41

1 Answers1

1

Yes, the call gets executed twice. You can use angular's built in $http cache option, or you could use an existing module like angular-cache, or other libraries such as Breeze or Amplify. You can also try handling it yourself, probably the worst option.

Mohammad Sepahvand
  • 17,364
  • 22
  • 81
  • 122
  • Thanks! But sometimes the data changes and caching is not the best solution. In this example http://bit.ly/1o7f1fk, you see that if this.busy is true it doesn't make subsequent calls. Do you think it's a good practice? – jamiltz Feb 09 '14 at 18:06
  • @jamiltz , that busy flag is to check whether a $http call is already in progress, if it is then no more data is retrieved. not sure how that relates to your original question, but in this case i think it's being done since multiple scroll events could be firing at the same time. – Mohammad Sepahvand Feb 09 '14 at 18:23