Description

The marshaling library converts collections of JS objects into a BISON format, allowing them to be stored in localStorage. This data may subsequently be read and the original objects reconstituted.

BISON : https://github.com/BonsaiDen/BiSON.js/ Inspired of Ruby : http://www.ruby-doc.org/core-1.9.3/Marshal.html

Example

To write :

Class.create("Foo", {
    bar: 2
});
Class.create("Test", {
    qux: 5
});
var foo = Class.new("Foo"),
    test = Class.new("Test");
Marshal.dump(foo, "save_name");
test.qux = 10;
Marshal.dump(test, "save_name");

Read the save from above :

Class.create("Foo", {
    bar: 2
});
Class.create("Test", {
    qux: 5
});
var foo = Class.new("Foo"),
    test = Class.new("Test");
foo = Marshal.load("save_name");
test = Marshal.load("save_name");
console.log(test.qux); // 10

With Node.js (>=1.3.1)

var CE = require("canvasengine").listen(8333),
    Marshal = CE.Core.requireExtend('Marshal'),
    Class = CE.Class;

CE.Model.init("Main", {

    Marshal: null,

    initialize: function() {
        // Create an instance only to the user
        this.Marshal = Class.New('Marshal');
    },

    save: function() {
        Class.create("Foo", {
            bar: 2
        });
        var foo = Class.new("Foo");
        this.Marshal.dump(foo, "save_name");

        // Retrieves the encoded classes. It remains to store (database, file, etc..)
        var data = this.Marshal.getStack(true);
    }

});

Methods

exist

Testing the existence of save in localStorage

load

Load data and restores the properties of the class the order of the stack of Marshal

dump

Saves the properties of a class in localStorage. The order is important for recovery with load method

getStack

`(>=1.3.1)` Retrieves the saved stack classes

remove

Remove save

Discussion