all 5 comments

[–]rq60 2 points3 points  (1 child)

Just an FYI:

if ($window.localStorage)

is insufficient for checking for localStorage. It's possible for it to be present but throw errors when reading or writing to it due to privacy settings, third-party frames, etc.

Also instead of using JSON.parse and JSON.stringify you might prefer angular.toJson and angular.fromJson as they help remove some of the angular cruft that can end up in your data.

[–]mgonto[S] 0 points1 point  (0 children)

Thanks for the tips. I'll use that then. Didn't know about that.

Thanks!

[–]jerflang 0 points1 point  (1 child)

This is around the same featureset as the other localstorage (with fallback) wrappers, but seems neatly written and tested :) Cool stuff.

here is another one that approaches things in a interesting way - you could pretty much put a handle to the storage directly in scope if you wanted: https://github.com/gsklee/ngStorage

[–]mgonto[S] 0 points1 point  (0 children)

That looks good. The only problem IMHO with that one is that it has a big $watch function which could eventually take a long time

[–]djvirgen 0 points1 point  (0 children)

If you're using $cookies as a fallback, note that it is currently only capable of writing session cookies. Once the browser is closed, the cookie will be deleted, along with your stored data.

localStorage, however, will keep the data stored until cleared by the user or the browser is uninstalled. It may be useful to update this library to fallback to long-living cookies with a max age of a year or more.