Giter Site home page Giter Site logo

crystalnix / backbone-idb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vincentmac/backbone-idb

0.0 23.0 2.0 29 KB

Backbone IndexedDB adapter with cross browser support via IDBWrapper

License: MIT License

JavaScript 95.12% Makefile 0.62% HTML 4.25%

backbone-idb's Introduction

backbone-idb

Backbone IndexedDB adapter with cross browser support via IDBWrapper

Build Status

IDBWrapper tutorial Part 1 - Basic CRUD

IDBWrapper tutorial Part 2 - Indexes and Query

See tests for usage until I get some more time to fully document.

Dependencies

Obtaining backbone-idb

Available via npm

$ npm install backbone-idb
# or
$ npm install backbone-idb --save # to install and save to package.json

Also available via bower

$ bower install backbone-idb
# or
$ bower install backbone-idb --save # to install and save to bower.json

Usage

Define a Backbone.Model or Backbone.Collection with an indexedDB property in the initialize function.

var Note = Backbone.Model.extend({});

var Notes = Backbone.Collection.extend({
  initialize: function() {
    this.indexedDB = new Backbone.IndexedDB({
      storeName: 'notes',
      dbVersion: 1,
      keyPath: 'id',
      autoIncrement: true,
      indexes:[
        {name:'tags', keyPath:'tags', unique: false, multiEntry: true},
        // When specifying only the `name`, `keyPath` is assummed to be the same.
        // `unique` and `multiEntry` are also false by default.
        // {name:'titleLC', keyPath:'titleLC', unique: false, multiEntry: false}
        {name:'titleLC'}
      ]
    }, this);
  },

  model: Note
});

The first parameter passed into Backbone.IndexedDB is the options object. You may pass in an empty object and have the default attributes set for the store (defaults listed below). Any options that you pass in will override the defaults.

// The default options object set on the Collection/Model
var options = {
  storeName: 'Store',
  storePrefix: '',
  dbVersion: 1,
  keyPath: 'id',
  autoIncrement: true,
  onStoreReady: defaultReadyHandler,
  onError: defaultErrorHandler,
  indexes: []
};

Since indexedDB is asynchronous in nature, we need to update the way we instantiate a new Collection or Model. By default, backbone-idb will trigger idb:ready on the object. This behaviour can be overridden by setting your own callback function on the onStoreReady attribute in the options object.

var notes = new Notes();

notes.once('idb:ready', function() {
  // Some actions to take after initializing the new collection
});

Collections/Models can now use the same Backbone.sync api to interact with IndexedDB; however, you will need to add your own success callback and, optionally, your own error callback in the options parameter.

notes.fetch({success: function() {
  // fetch success handler
}});

var note = new Note();

notes.add(note);
note.save({title: 'some note title'}, {success: function() {
  // save success handler
}});

TODO

  • Document retrieving models from a store by an index via the iterate command
  • Document keyRanges
  • Proxying directly to idb-wrapper via the indexedDB.store object

backbone-idb's People

Contributors

vincentmac avatar

Watchers

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