Closed richardgsands closed 9 years ago
If you don't intend to pass in references wouldn't it be sensible to pass in the copied object to $default ?
Thanks for your feedback egilkh. I did consider doing that, but I thinks it's far neater that the copy happens from within ngStorage for two reasons:
1) Semantically, if I define an object called defaultDataStructure
and initialise my storage with that, it is messy if that object gets mutated.
var defaultDataStructure = {
prop1: "value1",
prop2: {
prop2a: "value2a",
prop2b: "value2b",
}
};
var $myStorage = $localStorage.$default( defaultDataStructure );
If then do something like $storage.prop2.prop2a = "value2a_changed"
, I wouldn't expect defaultDataStructure to get changed.
2) If the $myStorage object is already persisted to local storage when the apps loads, the defaultDataStructure will not be used (because the values are fetched from localStorage).
That means the defaultDataStructure object would not be mutated by changes to $myStorage in that case anyway.
This pull request just makes it work the same way when localstorage is empty and it gets loaded for the first time.
Would welcome your thoughts on those points.
Hey! Sorry for the late response as I have been traveling.
I agree with both of your points! Thanks for the PR!
My pleasure - and thanks for looking after a very handy angular module!
Using angular.copy to set items on $storage in $default function. This is so that the object passed in to $default or $reset is not referenced, so it will not be affect by future changes to the $storage object (which is useful for example when syncing data, comparing current storage object against the default).