serializable-es5
A serialization utility that automatically serialize and de-serialize Class instance. It makes saving and restoring process of modular application much easier.
Browser support
Desktop: Chrome, Safari, Firefox, Opera, IE9+
Installation
npm install serializable-es5 or jspm install npm:serializable-es5
Usage
An example :
var Serializable = ; /** * Assume we are developing a shopping website, * And we have a page contains a list of items * that a user is going to buy. */ var { this_items = ; // Cart items. this_parent = domParent; this; }; /** * Property '_serializeInfo' must be provided because * Serializable utility recognize an object as serializable by * checking the existence of this property. */ Cart_serializeInfo = '_items' '_dataTable' '_nonExist' // Non-exist property will be skipped during serialization. '_finalize' ; Cartprototype = { /** * serialize() method takes two arguments : * argument 1. it is usually the instance itself. * argument 2. Class name in String. */ return Serializable; } { /** * You can ignore this. */ this_domContainer = ; this_header = text'My Cart'; /** * Create DataTable instance. */ this_dataTable = this_domContainer; } /** * Add an item to the list. */ { this_items; this; return this; } { this_dataTable; // Reload data table with new list. } /** * If user changed row-alternative-color settings. * Reload data table to reflect this change. * * You can ignore this method, just remember this setting is * important to the users and we will save it. */ { if typeof enableAltRowColor === 'boolean' if this_dataTable !== enableAltRowColor this_dataTable; this; else if typeof enableAltRowColor !== 'undefined' throw "IllegalArgumentException: enableAltRowColor must be a Boolean."; return this; } /** * This method will only be called during deserialization. * This is useful after all data is restored from deserialization and * you might want to do some last step operation, e.g. resize this * instance, etc. * * ( A function which has a name with prefix '_serialized' will be * called during both serialization and deserialization, otherwise it * will only be called during deserialization. ) */ { this; // Items are already restored we reload the list. this; // Data list are loaded, you might want to resize. } ; /** * DataTable class is incomplete, but since I am just showing * the usage of Serializable utility, I am not going to complete * this class. */ var { this_altRowColor = false; }; DataTable_serializeInfo = '_serializedAltRowColor' ; DataTableprototype = { if typeof isAltRowColor === 'boolean' this_altRowColor = isAltRowColor; else return this_altRowColor; } /** * Assume this method will load all data to doms and * show it to user. */ { } /** * You don't need to call this method explicitly, * this method will be called when 'Serializable' utility recognizes that * the value of Cart._dataTable is an instance of DataTable class which * implemented serializable interface ( has '_serializeInfo' ). */ { return Serializable; } /** * This method will be called during both serialization and deserialization. */ { /** * Deserialization. */ if typeof serialized === 'boolean' this_altRowColor = serialized; /** * You might add more code here. */ /** * serialization. */ else return this_altRowColor; } ; // Create Cart instance. var cart = documentbody; // Fake data. var { return type: 'fruit' weight: '1kg' name: name price: price ; }; // Assume we have a button. ; cart ; /** * Now you have a copy of serialized cart data, * you can save it to a database table or anywhere you want. */ var dataToSave = JSON; /** * Restore data is as easy as serialization. * @return */ var cart = Serializable; /** * You can also provide a cart instance as the 2nd argument, * but this is optional. If omitted, a cart instance will be * created during deserialization and finally returned. If provided, * the same instance will be returned. */ var cart = Serializable;
Public properties and methods :
Tests
npm test
Todo
- Add the document of public interfaces 🎈
- Add test.