HTML5 development-lightweight JSON storage solution lawnchair. js

Source: Internet
Author: User
Tags web database

Lawnchair is a lightweight data persistence storage solution for mobile applications. It is also a client-side JSON document storage method. It has the advantages of being short, simple syntax, and good scalability.

Currently, in addition to better compatibility with localstorage, both SQL Web Database and indexeddb are deadlocked. Although some people complain that "we should kill localstorage API, there is no choice now.

Lawnchair has a previous official website: http://westcoastlogic.com/lawnchair/. when the source code of this website is too long, there are still errors.

If you need to download the latest version in https://github.com/brianleroux/lawnchair

 

Application Example [Dom storage is applied ]:

VaR store = new lawnchair ({Name: 'testing'}, function (store ){
// Object 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 ){
// Object to be saved
VaR me = {key: 'Brian '};
// Save
Store. Save (me );

Store. Get ('Brian ', function (me ){
Console. Log (me );
});
});

Because the safe constructor is used, the effects of the two methods are the same. The first parameter of the callback function is the same object as the returned store. This can also be used inside the callback function.

Initialization:

var store = new Lawnchair(option,callback);

Option is an empty object by default. It has three optional attributes:

Option = {
Name: // equivalent to the table name
Record ://
Adapter: // storage type
}

The first parameter of callback is the current object, which can be replaced by this in the callback function.

API:

Keys (callback) // return all keyssave (OBJ, callback) of the stored object // save an object batch (array, callback) // save a set of objects get (Key | array, callback) // obtain one or more objects, call callback to process exists (Key, callback) // check whether a key exists, and return a Boolean value (true/false) pass to the callback function each (callback) // traverses the set and passes (object, object index) to the callback function all (callback) // put all objects in an array and return remove (Key | array, callback) // remove one or more 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 the callback function, even if it is null.

Save (OBJ, callback) // 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 a lawnchair object, if the input option parameter contains the name attribute, an array similar to table. _ index _ will be created to save the index value.

The storage format is an object. If the input object has the key attribute, the key is saved as the index value. If the input object does not have the key attribute, a key value is automatically generated and saved in table. in _ index _, the Operation Result of the preceding example is as follows:

Batch (array, callback) // save a group of Objects

The above example uses the batch method as follows:

    var store = Lawnchair({name:'table'}, function(store) {
});
store.batch([{
key:'hust',
name:'xesam_1'
},{
key:'whu',
name:'xesam_2'
}])
Exists (Key, callback) // check whether a key exists and pass the Boolean value (true/false) of the result to the callback function.
    store.exists('whu',function(result){
console.log(result);//true
})
store.exists('test',function(result){
console.log(result);//false
})
Get (Key | array, callback) // gets one or more objects and calls callback for processing.
    store.get('hust',function(result){
console.log(result);//{key:'hust',name:'xesam_1'}
})
All (callback) // put all objects in an array and return
    store.all(function(result){
console.log(result);//[{key:'hust',name:'xesam_1'},{key:'whu',name:'xesam_2'}]
})
Each (callback) // traverses the set and passes (object, object index) to the callback function.
    store.each(function(result){
console.log(result);
//{key:'hust',name:'xesam_1'}
// {key:'whu',name:'xesam_2'}
})
Remove (Key | array, callback) // remove one or more elements.
    store.remove('whu',function(){
store.all(function(result){
console.log(result)//[{key:'hust',name:'xesam_1'}]
});
})
Nuke (callback) // destroy all
    store.nuke(function(){
store.all(function(result){
console.log(result)//[]
});
})
Keys (callback) // return all keys of the stored object
    store.keys(function(result){
console.log(result)//['hust','whu']
})

 

The core of lawnchair. JS is very small, and there is a sound extension and plug-in mechanism that can be loaded as needed. You can easily write your own code by implementing the adapter valid init keys save batch get exists all remove nuke method in your code.

 

For more information, see http://www.cnblogs.com/xesam /]
Address: http://www.cnblogs.com/xesam/archive/2012/03/01/2370307.html

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.