This library allows javascript objects to be saved for later use, such as between page requests or closing and opening the browser.
Copy and require lib/perseverance.js
into your application.
Saving can be done by using the following:
Perseverance.save("item", myItem);
Your object can contain references to other objects, including itself, and that's ok. For instance the following should work fine:
var obj = new MyObject();
obj.someAttr = obj;
Perseverance.save("circular", obj);
There are cases however, when you may want to filter out certain elements from being saved. You may do so through a callback:
Perseverance.save("item", myItem, function(object) {
if(typeof(object) == "object") {
var klassName = Perseverance.getClassName(object);
// Avoid serializing third party objects
if(!klassName || klassName.match(/svg/i)) return false;
}
return object;
});
Loading the object is pretty straightforward:
var myItem = Perseverance.read("item");
Removing is as easy as:
Perseverance.delete("item");
The data is serialized as JSON into localStorage
and rebuilt using the __proto__
attribute. It does not work in Internet Explorer. However since this attribute has been standardized in EcmaScript 6, IE11 should run ok.
This package is licensed under the MIT license and/or the Creative Commons Attribution-ShareAlike.