A singleton object to access the device localStorage area
Example
// Initialize "score" and "lives" with default values
// This loads the properties from localStorage if they exist, else it sets the given defaults
me.save.add({ score : 0, lives : 3 });
// Print all
// On first load, this prints { score : 0, lives : 3 }
// On further reloads, it prints { score : 31337, lives : 3, complexObject : ... }
// Because the following changes will be saved to localStorage
console.log(JSON.stringify(me.save));
// Save score
me.save.score = 31337;
// Also supports complex objects thanks to the JSON backend
me.save.add({ complexObject : {} })
me.save.complexObject = { a : "b", c : [ 1, 2, 3, "d" ], e : { f : [{}] } };
// WARNING: Do not set any child properties of complex objects directly!
// Changes made that way will not save. Always set the entire object value at once.
// If you cannot live with this limitation, there's a workaround:
me.save.complexObject.c.push("foo"); // Modify a child property
me.save.complexObject = me.save.complexObject; // Save the entire object!
// Remove "lives" from localStorage
me.save.remove("lives");
Methods
-
static add(props)
-
Add new keys to localStorage and set them to the given default values if they do not exist
Parameters:
Name Type Description props
Object key and corresponding values
Example
// Initialize "score" and "lives" with default values me.save.add({ score : 0, lives : 3 });
-
static remove(key)
-
Remove a key from localStorage
Parameters:
Name Type Description key
String key to be removed
Example
// Remove the "score" key from localStorage me.save.remove("score");