<script src="localforage.js"></script>
。你不需要运行任何初始化方法或等待 onready 事件。localStorage
-like API. It allows developers to store many types of data instead of just strings.npm install localforage
) or bower (bower install localforage
).<script src="localforage.js"></script>
. You don't need to run any init method or wait for any onready
events.getItem(key, successCallback)
getItem()
will return null
.setItem(key, value, successCallback)
Array
ArrayBuffer
Blob
Float32Array
Float64Array
Int8Array
Int16Array
Int32Array
Number
Object
Uint8Array
Uint8ClampedArray
Uint16Array
Uint32Array
String
removeItem(key, successCallback)
clear(successCallback)
length(successCallback)
key(keyIndex, successCallback)
keys(successCallback)
iterate(iteratorCallback, successCallback)
iteratorCallback
is called once for each pair, with the following arguments:iterate
supports early exit by returning non `undefined` value inside `iteratorCallback` callback. Resulting value will be passed to `successCallback` as the result of iteration. This means if you're using CoffeeScript, you'll need to manually `return` nothing to keep iterating through each key/value pair.getItem()
or length()
, etc.)setDriver(driverName)
setDriver([driverName, nextDriverName])
setDriver()
with one or more of the following arguments:localforage.INDEXEDDB
localforage.WEBSQL
localforage.LOCALSTORAGE
config(options)
config()
then setDriver()
. The following config values can be set:setDriver
, above.
Default: [localforage.INDEXEDDB, localforage.WEBSQL, localforage.LOCALSTORAGE]
name The name of the database. May appear during storage limit prompts. Useful to use the name of your app here. In localStorage, this is used as a key prefix for all keys stored in localStorage.
Default: 'localforage'
size The size of the database in bytes. Used only in WebSQL for now.
Default: 4980736
storeName The name of the datastore. In IndexedDB this is the dataStore
, in WebSQL this is the name of the key/value table in the database. Must be alphanumeric, with underscores. Any non-alphanumeric characters will be converted to underscores.
Default: 'keyvaluepairs'
version The schema version of your database. Used only in WebSQL and IndexedDB. In WebSQL, this simply sets the version, and in IndexedDB this may trigger an onupgradeneeded
event if a version upgrade is detected. If a new store is detected, localForage will ask IndexedDB to increment the version itself to manually trigger the onupgradeneeded
event. As of right now, upgrade events are not customizable, but may be in the future. For drivers that do not support configuration for versioning, this value simply gets thrown away.
Default: 1.0
description A description of the database, essentially for developer usage.
Default: ''
config
method is synchronous.callback
argument and that you pass the same arguments to callbacks as the default drivers do. You'll also want to resolve or reject promises. Check any of the default drivers for an idea of how to implement your own, custom driver._support
property that is either boolean (true
/false
) or returns a Promise
that resolves to a boolean value. If _support
is omitted, then true
is the default value. You can use this to make sure the browser in use supports your custom driver.driver()
null
during the asynchronous driver initialization process (see ready
for more details), or null
if the asynchronous driver initialization process failed to find a usable driver.ready()
provides a way to determine whether the asynchronous driver initialization process has finished. That's useful in cases like when we want to know which driver localForage has settled down using.supports(driverName)
driverName
is supported by the browser.name
and storeName
properties, it drops the specified "store". When invoked with an object specifying only a name
property, it drops the specified "database" (and all its stores).