2

In non-trivial applications, I usually end up having a "dumb JS object" that has nothing but data in it... a data structure. Might as well have been stored as JSON somewhere and given to me when needed. But in this case an AJAX request for JSON might be unwarranted.

Example: the ng-options directive in AngularJS for dropdowns. I need to give it an array of objects, but storing that in my controller clutters it with a data structure that might well have been invisible most of the time. Many such cases, even outside angular.

One solution I can think of creating a file that does nothing but store and retrieve these objects and use that across the project, but that's unnecessary network and memory consumption (data that has no bearing on the current context is being fetched).

Another solution would be to make these objects at the bottom of the file. But they either need to be on the window object, or I'll need to write the boilerplate for storing and fetching objects in each file I work with (especially when using RequireJS). I could store this "storer/fetcher" on the window object and pass it the JSON in each file, but I feel there is room for improvement.

What would you suggest?

Aditya M P
  • 4,493
  • 6
  • 33
  • 69
  • Update: was suggested Cenny.js, looks good, though we still need a strategy to avoid problems in this question. – Aditya M P Dec 12 '13 at 08:19

1 Answers1

0

Have you thought about using localStorage? Here is explained how to correctly use it with objects. Storing Objects in HTML5 localStorage

Community
  • 1
  • 1
Alexander Nenkov
  • 2,872
  • 17
  • 27
  • Hmm. What would be a good strategy to get those objects into localStorage in the first place? I'm thinking, for example, in a requirejs project: on app start in the bootstrap file. It would just mean that on app start, data for the entire app would still be downloaded, though (whether or not those pages are visited). – Aditya M P Dec 09 '13 at 10:53
  • Well, you can download it once and later just check that data is there. Local storage is somewhat persistent. http://stackoverflow.com/questions/9948284/how-persistent-is-localstorage – Alexander Nenkov Dec 09 '13 at 13:03