GithubHelp home page GithubHelp logo

arkadiuszsz / localforage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from localforage/localforage

0.0 1.0 0.0 5.01 MB

Offline storage, improved. Wraps IndexedDB, WebSQL, or localStorage using a simple but powerful API.

Home Page: https://mozilla.github.io/localForage

License: Other

Ruby 0.87% JavaScript 92.53% HTML 3.44% CSS 3.16%

localforage's Introduction

localForage Build Status

localForage is a fast and simple storage library for JavaScript. localForage improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API.

localForage uses localStorage in browsers with no IndexedDB or WebSQL support. See the wiki for detailed compatibility info.

To use localForage, just drop a single JavaScript file into your page:

<script src="localforage.js"></script>
<script>localforage.getItem('something', myCallback);</script>

Download the latest localForage from GitHub, or install with npm:

npm install localforage

or bower:

bower install localforage

localForage is compatible with browserify.

Support

Lost? Need help? Try the localForage API documentation.

If you're stuck using the library, running the tests, or want to contribute to localForage, you can visit irc.mozilla.org and head to the #apps channel to ask questions about localForage.

The best person to ask about localForage is tofumatt, who is usually online from 8am-8pm GMT (London Time).

How to use localForage

Callbacks

Because localForage uses async storage, it has an async API. It's otherwise exactly the same as the localStorage API.

// In localStorage, we would do:
localStorage.setItem('key', JSON.stringify('value'));
alert('value');

// With localForage, we use callbacks:
localforage.setItem('key', 'value', function(err, value) { alert(value); });

Similarly, please don't expect a return value from calls to localforage.getItem(). Instead, use a callback:

// Synchronous; slower!
var value = JSON.parse(localStorage.getItem('key'));
alert(value);

// Async, fast, and non-blocking!
localforage.getItem('key', function(err, value) { alert(value) });

Callbacks in localForage are Node-style (error argument first) since version 0.9.3. This means if you're using callbacks, your code should look like this:

// Use err as your first argument.
localforage.getItem('key', function(err, value) {
    if (err) {
        console.error('Oh noes!');
    } else {
        alert(value);
    }
});

You can store any type in localForage; you aren't limited to strings like in localStorage. Even if localStorage is your storage backend, localForage automatically does JSON.parse() and JSON.stringify() when getting/setting values.

Promises

Promises are pretty cool! If you'd rather use promises than callbacks, localForage supports that too:

function doSomethingElse(value) {
    console.log(value);
}

// With localForage, we allow promises:
localforage.setItem('key', 'value').then(doSomethingElse);

When using Promises, err is not the first argument passed to a function. Instead, you handle an error with the rejection part of the Promise:

// A full setItem() call with Promises.
localforage.setItem('key', 'value').then(function(value) {
    alert(value + ' was set!');
}, function(error) {
    console.error(error);
});

localForage relies on native ES6 Promises, but ships with an awesome polyfill for browsers that don't support ES6 Promises yet.

Storing Blobs, TypedArrays, and other JS objects

localForage supports storing all native JS objects that can be serialized to JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the API docs for a full list of types supported by localForage.

All types are supported in every storage backend, though storage limits in localStorage make storing many large Blobs impossible.

Configuration

You can set database information with the config() method. Available options are driver, name, storeName, version, size, and description.

Example:

localforage.config({
    driver      : localforage.WEBSQL, // Force WebSQL; same as using setDriver()
    name        : 'myApp',
    version     : 1.0,
    size        : 4980736, // Size of database, in bytes. WebSQL-only for now.
    storeName   : 'keyvaluepairs', // Should be alphanumeric, with underscores.
    description : 'some description'
});

Note: you must call config() before you interact with your data. This means calling config() before using getItem(), setItem(), removeItem(), clear(), key(), keys() or length().

RequireJS

You can use localForage with RequireJS:

define(['localforage'], function(localforage) {
    // As a callback:
    localforage.setItem('mykey', 'myvalue', console.log);

    // With a Promise:
    localforage.setItem('mykey', 'myvalue').then(console.log);
});

Web Workers

Web Worker support in Firefox is blocked by bug 701634. Until it is fixed, web workers are not officially supported by localForage.

Framework Support

If you use a framework listed, there's a localForage storage driver for the models in your framework so you can store data offline with localForage. We have drivers for the following frameworks:

If you have a driver you'd like listed, please open an issue to have it added to this list.

Custom Drivers

You can create your own driver if you want; see the defineDriver API docs.

There is a list of custom drivers on the wiki.

Working on localForage

You'll need node/npm, bower, and Grunt.

To work on localForage, you should start by forking it and installing its dependencies. Replace USERNAME with your GitHub username and run the following:

git clone [email protected]:USERNAME/localForage.git
cd localForage
npm install
bower install

Omitting the bower dependencies will cause the tests to fail!

Running Tests

You need PhantomJS installed to run local tests. Run npm test (or, directly: grunt test). Your code must also pass the linter.

localForage is designed to run in the browser, so the tests explicitly require a browser environment. Local tests are run on a headless WebKit (using PhantomJS).

When you submit a pull request, tests will be run against all browsers that localForage supports on Travis CI using Sauce Labs.

License

This program is free software; it is distributed under an Apache License.


Copyright (c) 2013-2015 Mozilla (Contributors).

localforage's People

Contributors

adambutler avatar adig avatar ahanriat avatar albertogasparin avatar alexandremottet avatar beng-hee-eu avatar blackglory avatar code-vicar avatar davidguttman avatar evgenus avatar gyoshev avatar iamolivinius avatar jviereck avatar lejenome avatar lu4 avatar magalhas avatar marcucio avatar nantunes avatar nolanlawson avatar ocombe avatar peterbe avatar pgherveou avatar psalaets avatar sole avatar stephanebachelier avatar thgreasi avatar thisandagain avatar tofumatt avatar tombyrer avatar wmluke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.