Skip to content

Instantly share code, notes, and snippets.

@hungryzi
Last active December 11, 2015 08:58
Show Gist options
  • Save hungryzi/4577117 to your computer and use it in GitHub Desktop.
Save hungryzi/4577117 to your computer and use it in GitHub Desktop.
Using neo/indexeddb-backbone-adapter
# Defining schema
IndexedDBBackbone.describe('todos')
.createStore('todos', { keyPath: 'id' })
.createIndex('todos', 'nameIndex', 'name', unique: false)
# Models & Collections
app.Todos = Backbone.Collection.extend
database: 'todos'
storeName: 'todos'
# ...
app.Todo = Backbone.Model.extend
database: 'todos'
storeName: 'todos'
# ...
# Querying
query = new IndexedDBBackbone.IDBQuery('todos', 'nameIndex')
.bounds("a", "f").limit(5).offset(10)
todos = new app.Todos()
todos.fetch
query: query
success: ->
# ...
# Transaction
Backbone.transaction [accounts], () ->
accountA.withdraw(amount);
accountB.deposit(amount);
if (something_goes_wrong)
return false # this rollbacks the transaction
else
return true # this commits the transaction,
, {
success: ->
# successful
error: ->
# something went wrong. everything's rollbacked. $ stay where it was.
abort: ->
# everything's rollbacked. $ stay where it was
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment