Persists javascript objects between page requests
JavaScript CSS
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
LICENSE
README.md

README.md

Perseverance

This library allows javascript objects to be saved for later use, such as between page requests or closing and opening the browser.

Installation

Copy and require lib/perseverance.js into your application.

Usage

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");

Additional notes

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.

License

This package is licensed under the MIT license and/or the Creative Commons Attribution-ShareAlike.