Keep your valuables well stored.
Wallet is a tool that wraps Mozilla's LocalForage to simplify offline storage.
- Easy Key-Value pairs based API
- Uses IndexedDB in the "backstage"
- Allows the storing of all types of Javascript objects
- Object
- String
- Number
- ArrayBuffer
- Accepts either
function(){}
callbacks and SuitJS ControllerNotifications - Offers database contexts to avoid data collision between applications
- Download either the
wallet.js
orwallet.min.js
sources. - Add the tag
<script src="js/suitjs/wallet.js"></script>
- Wallet is available as bower package.
- Run
bower install suitjs-wallet
- It will install all script versions.
- Add the tag
<script src="bower_components/suitjs-wallet/js/wallet.js"></script>
- TBD
- Run
npm run init
once npm run build
After adding the script tag, the Wallet
global variable will be available.
//Before the 'load' event, call 'Wallet.init' and set your database name.
//Try to make this name unique because it may collide with other apps using 'Wallet'.
Wallet.init("my-unique-database");
//Not obligatory.
//But a context can be set and allow different kinds of profiling.
Wallet.context = "debug";
//Set some value.
//Internally the 'key' used will be 'debug.key'
Wallet.set("key",{ "some": "data", "num": 10}, function(p_data,p_error) {
if(p_error!=null) { console.error(p_error); return; } //something went wrong.
console.log(p_data); //object we just stored
});
/*...*/
Wallet.get("key",function(p_data,p_error) {
if(p_error!=null) { console.error(p_error); return; } //something went wrong.
console.log(p_data); //{ "some": "data", "num": 10}
});
Then, it is also possible to integrate Wallet
into SuitJS applications.
Make sure SuitJS
exists in the application context.
//Setup Wallet as above
//Define a simple controller.
Suit.controller.add({
on: function(n) {
switch(n.path) {
case "storage-update@set": console.log(n.data); /*{ "some": "data", "num": 10}*/ break;
case "storage-update@error": console.log(n.data); /*Error*/ break;
case "storage-fetch@get": console.log(n.data); /*{ "some": "data", "num": 10}*/ break;
case "storage-fetch@error": console.log(n.data); /*Error*/ break;
}
}
});
//Call the same methods but use String as 'callback'.
//They will do a Suit.controller.dispatch()
Wallet.set("key",{ "some": "data", "num": 10},"storage-update");
Wallet.get("key","storage-fetch");
For in depth information of the API, visit the documentation.
Usage examples can be found at CodePen.