Re: [angular.js] Use $route to control state, but don't hit the server when we don't need to

3,572 views
Skip to first unread message

John Lindquist

unread,
Jun 26, 2012, 12:40:51 PM6/26/12
to ang...@googlegroups.com
Here's my take:

Write wrapper for the Service
Cache results in the wrapper
Allow the controller to make the call, but return cached results if they exist

On Tue, Jun 26, 2012 at 10:24 AM, Sean Hess <sean...@gmail.com> wrote:
I'm playing around with my first angular app. I'm super happy with how easy it is. 

I am making an app with a list of characters on the left, and a new/edit form on the right. When the user clicks on a character, I want it to populate the form with the characters info. 

First, I had the form request the character by id from the server. That works fine, but I don't want to make that call to the server. I already have all the data in the list. I want to pass it in. 

Second, I had the list update a .selected variable on a service shared between the list and the form. That worked fine, but I lost deep linking. 

Third, I got the router to control the selection, but it has to re-initialize the list controller each time they click, which calls the server to get the whole list. 

(Current = Third take) https://gist.github.com/2996792

How can I make the form use the list, without sending any new requests, and still get the router to work?

Thanks!
~sean


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/7wsnjCggjkUJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

John Lindquist

unread,
Jun 26, 2012, 1:57:51 PM6/26/12
to ang...@googlegroups.com
Right. I prefer to think of angular controllers as dumb apis/configs for my views and leave all the heavy lifting to services. I think everyone will have their own opinions as to the balance between relying on $scope, separation of services, defining tons of directives, etc.

On Tue, Jun 26, 2012 at 11:20 AM, Sean Hess <sean...@gmail.com> wrote:
Hi John, we keep running into each other :)

Ok, so I let it re-initialize the controller, but since it re-injects the same wrapper as before, I can have it intelligently decide NOT to re-request the data. Cool. That's a good idea. 

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/VZ4zsqWC7a8J.

John Lindquist

unread,
Jul 5, 2012, 4:45:40 PM7/5/12
to ang...@googlegroups.com
The "Wire up a Backend" example on AngularJS.org has a $scope.isClean check that compares the current data set to see if it has changed. That's very similar to what we're talking about, but just putting it in the Service instead of the Controller.

On Thu, Jul 5, 2012 at 12:31 PM, Eamonn O'Connell <eamonno...@gmail.com> wrote:
 This is useful. I wonder, this seems like such a common use case, are there any examples, or other sources on this. 

On Tuesday, 26 June 2012 19:57:51 UTC+2, John Lindquist wrote:
Right. I prefer to think of angular controllers as dumb apis/configs for my views and leave all the heavy lifting to services. I think everyone will have their own opinions as to the balance between relying on $scope, separation of services, defining tons of directives, etc.

On Tue, Jun 26, 2012 at 11:20 AM, Sean Hess <> wrote:
Hi John, we keep running into each other :)

Ok, so I let it re-initialize the controller, but since it re-injects the same wrapper as before, I can have it intelligently decide NOT to re-request the data. Cool. That's a good idea. 

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/VZ4zsqWC7a8J.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6pBSzPIqBNEJ.
Reply all
Reply to author
Forward
0 new messages