Best Practice to handle app-wide static data, like dropdown list values?

976 views
Skip to first unread message

Matt Kruse

unread,
Feb 20, 2013, 2:50:10 PM2/20/13
to ang...@googlegroups.com
Most apps contain static data, such as dropdowns of countries or states. What is the best way to store this data so it can be used in any template?

Should I create a service which provides the list values? Or load them into $rootScope? Or some other way?

Let's say I have a complex app that may have hundreds of static lists. How would I lazy-load those lists on demand, rather than having to bulk-load them on app startup?

And because apps are not so easy, let's say I have server-side admin controls to manipulate those list values. So the admin might make a new option for "car color" - how do I make sure the app reflects the change as soon as possible, rather than caching the values indefinitely or waiting for a page reload?

Thanks,

Matt Kruse

Michael Bielski

unread,
Feb 20, 2013, 3:38:18 PM2/20/13
to ang...@googlegroups.com
There are any number of data-persistence libraries out there, some of which can be successfully linked into Angular. I still tend to fall back on localStorage/sessionStorage because I can update that data from the server behind the scenes (either upon login or via a web-worker.) Of course, getting that to update on the page requires that localStorage/sessionStorage be wrapped in Angular, and I haven't gotten that far yet. If you do, I suspect that several people (including myself) would appreciate it if you shared your results.

Joshua Miller

unread,
Feb 20, 2013, 4:23:27 PM2/20/13
to angular
Hello!

I think your gut is right about using a service. I would also fetch it with $http lazily so it doesn't get downloaded until its needed.

You could also ensure the file is downloaded only once. This is crazy-simple to implement either manually of with $http's cache option.

As for updating the file, I think this should be done server-side, where you can control caching on the file for all users. When the file has not been updated, the server can respond with "304 Not Modified" and the browser will not redownload it. Obviously this and the previous solution are mutually exclusive.

You could also poll the server for changes prior to fetching the file and then only fetch the file if you need to.

Josh


On Wed, Feb 20, 2013 at 12:38 PM, Michael Bielski <michael...@yahoo.com> wrote:
There are any number of data-persistence libraries out there, some of which can be successfully linked into Angular. I still tend to fall back on localStorage/sessionStorage because I can update that data from the server behind the scenes (either upon login or via a web-worker.) Of course, getting that to update on the page requires that localStorage/sessionStorage be wrapped in Angular, and I haven't gotten that far yet. If you do, I suspect that several people (including myself) would appreciate it if you shared your results.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages