localForage
A Polyfill providing a simple name:value syntax for client-side data storage, which uses IndexedDB in the background, but falls back to WebSQL and then localStorage in browsers that don't support IndexedDB.
Snippet:
// ## setItem(key, item, successCallback) // item is javascript object // With localForage, we use callbacks: localforage.setItem('key', 'value', doSomethingElse); // Or we can use Promises: localforage.setItem('key', 'value').then(doSomethingElse); // Unlike localStorage, you can store non-strings. localforage.setItem('my array', [1, 2, 'three'], function(err, item) { // This will output `1`. console.log(item[0]); }); // ## getItem(key, successCallback) localforage.getItem('somekey', function(err, value) { // Run this code once the value has been // loaded from the offline store. console.log(value); }); localforage.getItem('somekey').then(function(value) { // The same code, but using ES6 Promises. console.log(value); }); // ## remove all item localforage.clear(function(err) { // Run this code once the database has been entirely deleted. console.log('Database is now empty.'); }); // ## Get the list of all keys in the datastore. localforage.keys(function(err, keys) { // An array of all the key names. console.log(keys); }); // // ## Implement the driver here. var myCustomDriver = { _driver: 'customDriverUniqueName', _initStorage: function(options) { // Custom implementation here... }, clear: function(callback) { // Custom implementation here... }, getItem: function(key, callback) { // Custom implementation here... }, key: function(n, callback) { // Custom implementation here... }, keys: function(callback) { // Custom implementation here... }, length: function(callback) { // Custom implementation here... }, removeItem: function(key, callback) { // Custom implementation here... }, setItem: function(key, value, callback) { // Custom implementation here... } } // Add the driver to localForage. localforage.defineDriver(myCustomDriver);
Other ref: zh-TW
No comments:
Post a Comment