Coder Social home page Coder Social logo

backbone-localstorage's Introduction

Backbone localStorage

Build Status

Installing Backbone localStorage

The package can be installed through npm or bower.

Using npm
npm install backbone-localstorage
Using bower
bower install backbone-localstorage

After installing the package, you can grab the backbone-localstorage.js or the minified version of it backbone-localstorage.min.js

Using Backbone localStorage

When data is fetched via Backbone.sync (models & collections), the reponse will be stored with JSON.stringify in localStorage (before any success method is called). The name under the data is stored is in the following format:

<prefix>:<url>

Where prefix is the value set by Backbone.LocalStorage.setPrefix (default unknown) and where url is the remote url from a model or collection.

Using Backbone.Model
var model = Backbone.Model.extend({
    urlRoot: '/api/pages',
    localStorage: true
});
Using Backbone.Collection
var collection = Backbone.Collection.extend({
    url: '/api/pages',
    localStorage: true
});
Using forceRefresh option
var collection = Backbone.Collection.extend({
    url: '/api/pages',
    localStorage: true
});

collection.fetch({forceRefresh:true});
Set a prefix
Backbone.LocalStorage.setPrefix('my-namespace');

Autoclear localStorage when new content is available

Since many web applications have managable content, it's hard to determine when to recognize new content. The only thing we know about our data is the url and it's timestamp when the data is strored in localStorage. With the method setVerion you can determine in which state of content the localStorage is set. At the point you call this method verions are compared (in strict mode). If the verion is not equal to it's, if present, previous version the localStorage will be cleared (only data set under prefix).

Backbone.LocalStorage.setVersion(100);

The value may be of any type (int, string, etc.).

Changelog

0.3.2

  • Fixed url property compatible as a function, not just as a string value

0.3.1

  • Fixed support of localStorage in case of iOS private mode
  • Added tests for storing objects and string (stringified or not)

0.3.0

  • Added forceRefresh option
  • Refactored id to url of model/collection

0.2.1

  • Fixed setVersion clear localStorage when verions differs

0.2.0

  • Fixed issue return null when no localStorage can be found
  • Added documentation

0.1.0 (released on friday, november 15, 2013)

  • Prototype

backbone-localstorage's People

Contributors

robmoorman avatar

Watchers

James Cloos avatar Vladimir Zaburdaev 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.