GithubHelp home page GithubHelp logo

simplestorage's Introduction

SimpleStorage

A javascript module to make HTML5 Web Storage API (localStorage and sessionStorage) easier to use.

The module is provided in the AMD way and should work well with module kernels like RequireJS or OzJS.

Insipred by CacheProvider.js.

Usage

new SimpleStorage(useWebStorageIfAvailable, type)
  • {Boolean} useWebStorageIfAvailable - optional, defaults to true. When true, SimpleStorage will use the HTML5 web storage API, if available.
  • {String} type - optional, defaults to 'local'. When 'session', SimpleStorage use sessionStorage instead of localStorage.

Let's say you name the module mod/simplestorage

define(['mod/simplestorage']), function(SimpleStorage) {
	// use sessionStorage if possible
	var cache = new SimpleStorage(true, 'session')
	cache.set('keyName1', 'some string')
	console.log(cache.get('keyName1')) // 'some string'
	cache.set('keyName2', {foo: ['bar']})
	console.log(cache.get('keyName2').foo) // ['bar']
}

BBStorage (backbone.storage.js)

This module enhance SimpleStorage with a usefull API to store Models and Collections of Backbone.

If you create your collection like this before:

var Library = Backbone.Collection.extend({
	model: Book,
	url: '/library'
})
var library = new Library([], {foo: 'bar'})
var doSomething = function() {..}
library.on('reset', doSomething)
library.fetch({reset: true})

You can store the collection in localStorage or sessionStorage with BBStorage like this:

define(['mod/backbone.storage']), function(BBStorage) {
	var cache = new BBStorage(true)
	var Library = Backbone.Collection.extend({
		model: Book,
		url: '/library'
	})
	var library = cache.getCollection('libraryName', Library, {foo: 'bar'})
	var doSomething = function() {..}
	library.on('reset', doSomething)
	if(library.length) {
		doSomething()
	} else {
		library.fetch({reset: true})
	}
}

library will use the data in localStorage instead of fecthing from the server in this way. Whenever you add some model (the 'add' event) or do fecth() again (the 'reset' event), the cache will be updated immediately.

And there's a similar API getModel for models of Backbone:

var book = cache.getModel('bookName', Book)

simplestorage's People

Contributors

mondaychen avatar

Stargazers

 avatar

Watchers

 avatar  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.