Giter Club home page Giter Club logo

indexeddb-backbonejs-adapter's Introduction

This is an IndexedDB adapter for Backbone.js.

Usage

We're using the legendary TodoMVC app to demonstrate the use of our adapter. Code can be found in examples/todo/ folder.

1. Defining your Schema

You will need to include gen/indexeddb-backbone.js in your project and define a schema.

IndexedDBBackbone.describe('todos-database')
  .createStore('todos', keyPath: 'id')

The above code defines a database named todos-database.

2. Using Backbone with indexedDB

In your Backbone models and collections, specify 2 more attributes: database and storeName.

app.Todos = Backbone.Collection.extend({
  database: 'todos',
  storeName: 'todos',
  ...
});

app.Todo = Backbone.Model.extend({
  database: 'todos',
  storeName: 'todos',
  ...
});

That's all. Your app now uses IndexedDB for storage.
Opening up Resources tab in Developer Tools in Chrome, refresh your IndexedDB and you should see something similar to this:

*Image

3. Transactions

Make use of Transactions!
Let's take a look at this classic transaction use example:

Backbone.transaction([accounts], function(){
  accountA.withdraw(amount);
  accountB.deposit(amount);

  if (something_goes_wrong){
    return false; // this rollbacks the transaction
  } else {
    return true; // this commits the transaction
  }
}, {
  success: function(e){
    // successful
  },
  error: function(e){
    // something went wrong. everything's rollbacked. $ stay where it was.
  },
  abort: function(e){
    // everything's rollbacked. $ stay where it was.
  }
});

Backbone.transaction(objects, run, options)

  • objects: an array of Backbone.Model or Backbone.Collection to know how many object stores it needs to touch.
  • run: a function enclosing all the operations you want to run in the transaction.
  • options: callbacks for success, error and abort.

Browser support and limitations

Contributing

Tests

We use Jasmine as our test framework. Specs are written in CoffeeScript in spec/ folder. To generate spec, run cake spec. Open SpecRunner.html to run the specs.

Contributing

Components of adapter are written in CoffeeScript in src/. cake watch watches changes made to this folder and spec/ and compiles to Javascript in gen/.

Credits

This project started as a fork from superfeedr/indexeddb-backbonejs-adapter.

indexeddb-backbonejs-adapter's People

Contributors

julien51 avatar lcalvy avatar chris-morgan avatar xaethos avatar odinho avatar winfred avatar hungryzi avatar edulan avatar lyleunderwood avatar paulkinlan avatar manuels avatar

Watchers

James Cloos avatar Kyaw Tun 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.