HTML5 Development--Lightweight JSON storage solution lawnchair.js

Source: Internet
Author: User
Tags web database

Lawnchair is a lightweight mobile application data persistence storage scheme, but also the client JSON document storage method, the advantages are short, simple syntax, extensibility is better.

Now do HTML5 mobile applications in addition to Localstorage compatibility is good, SQL Web database and INDEXEDDB are in deadlock, although some people shout "we should kill Localstorage API", but that is something, There is no choice now.

Lawnchair has a former official website: http://westcoastlogic.com/lawnchair/, but this site provides version of the source code is outdated, and there are errors.

If you need to download it, the latest version is Https://github.com/brianleroux/lawnchair

application Example "Dom Storage is applied":

var store = new Lawnchair ({name: ' testing '}, function (store) {
Objects that need to be saved
var me = {key: ' Brian '};
Save
Store.save (Me);

Store.get (' Brian ', function (me) {
Console.log (Me);
});
});

Or:

var store = Lawnchair ({name: ' testing '}, function (store) {
Objects that need to be saved
var me = {key: ' Brian '};
Save
Store.save (Me);

Store.get (' Brian ', function (me) {
Console.log (Me);
});
});

Because a safe constructor is used, the effect of the two methods is consistent. The first parameter of the callback function is the same object as the store returned, which can also be substituted within the callback function.

Initialization

var store = new Lawnchair (option,callback);

option defaults to an empty object, with three optional properties:

option = {
name://equivalent to table name
record://
adapter://Storage Type
}

The first parameter of the callback is the current object, which can also be substituted within the callback function.

Api:

Keys(Callback)//Return all keys for the storage objectSave(Obj,Callback)//Save an objectBatch(Array,Callback)//Save a group of objectsGet(Key| array, callback)//Get one or a group of objects, then call callback processing exists (key, callback)// Checks for the existence of a key and passes the Boolean value of the result (True/false) to the callback function each (callback)//Iterates through the collection, passing (object, object index) to the callback function  All (callback)//Put all objects in an array to return remove (key|  Array, callback)//Remove one or a group of elements. Nuke (callback)//Destroy All             

Initialization

var store = new Lawnchair ({name: ' Test '}, function () {});
Or
var store = new Lawnchair (function () {});

The parameter must have a function as a callback function, even if it is empty.

(objcallback)//Save an object  
    var store = Lawnchair ({name: ' table '}, function (store) {
});
Store.save ({
Key: ' Hust ',
Name: ' Xesam_1 '
})
Store.save ({
Key: ' Whu ',
Name: ' Xesam_2 '
})

When creating an Lawnchair object, if the incoming option parameter contains the name attribute, a table._index_-like array is created to hold the index value.

Save as the object, if the incoming object has a key property, then key will be saved as index value, if there is no key property, then automatically generate a key value, and then saved in Table._index_, the above example operation results such as:

Batch(arraycallback)//Save a group of objects   

The above example instead of using the batch method is:

    var store = Lawnchair ({name: ' table '}, function (store) {
});
Store.batch ([{
Key: ' Hust ',
Name: ' Xesam_1 '
},{
Key: ' Whu ',
Name: ' Xesam_2 '
}])
(keycallback)//check for the presence of a key and pass the Boolean value of the result (True/false) to the callback function  
    Store.exists (' Whu ', function (result) {
Console.log (result);//true
})
Store.exists (' Test ', function (result) {
Console.log (result);//false
})
(key|  Arraycallback)//Gets one or a group of objects and then calls callback processing    
    Store.get (' Hust ', function (result) {
Console.log (result);//{key: ' Hust ', Name: ' Xesam_1 '}
})
(callback)//Put all objects in an array to return 
    Store.all (function (result) {
Console.log (result);//[{key: ' Hust ', Name: ' Xesam_1 '},{key: ' Whu ', Name: ' Xesam_2 '}]
})
Each(callback)//Iterate through the collection, passing (object, object index) to the callback function  
    Store.each (function (result) {
Console.log (result);
{key: ' Hust ', Name: ' Xesam_1 '}
{key: ' Whu ', Name: ' Xesam_2 '}
})
(key|  Arraycallback)//Remove one or a group of elements. 
    Store.remove (' Whu ', function () {
Store.all (function (result) {
Console.log (Result)//[{key: ' Hust ', Name: ' Xesam_1 '}]
});
})
(callback)//Destroy All 
    Store.nuke (function () {
Store.all (function (result) {
Console.log (Result)//[]
});
})
(callback)//Return all keys of the storage object 
    Store.keys (function (result) {
Console.log (Result)//[' hust ', ' WHU ']
})

The core of Lawnchair.js is very small, then there is a perfect extension and plug-in mechanism that can be loaded on demand. It is also easier to write your own code, just implement adapter valid init keys save batch get exists all remove Nuke method.

HTML5 Development--Lightweight JSON storage solution lawnchair.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.