backbone-db
Advanced tools
Comparing version 0.2.1 to 0.3.0
var Backbone = require('backbone'); | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var debug = require('debug')('backbone-db'); | ||
@@ -95,3 +95,4 @@ var jsonquery = require('jsonquery'); | ||
} | ||
Backbone.Db = function Db(name) { | ||
var Db = Backbone.Db = function Db(name) { | ||
var self = this; | ||
@@ -107,39 +108,2 @@ if (!(self instanceof Db)) return new Db(name); | ||
Backbone.Db.prototype.sync = function(method, model, options) { | ||
options = options || {}; | ||
var self = this; | ||
var db; | ||
if(!(self instanceof Backbone.Db)) { | ||
db = model.db || options.db || new Backbone.Db(model.type || model.name || model.kind || "model"); | ||
} else { | ||
db = self; | ||
} | ||
debug("sync %s %s",method,JSON.stringify(options)); | ||
function callback(err, res, resp) { | ||
debug('callback '+err+" "+JSON.stringify(res)); | ||
if((err && options.error) || (!err && !res && options.error)) { | ||
err = err || new Error("not found"); | ||
return options.error(err, resp); | ||
} else if(options.success && res) { | ||
debug('success %s', JSON.stringify(res)); | ||
return options.success(res, resp); | ||
} | ||
} | ||
switch (method) { | ||
case 'create': | ||
return db.create(model, options, callback); | ||
case 'update': | ||
return db.update(model, options, callback); | ||
case 'delete': | ||
return db.destroy(model, options, callback); | ||
case 'read': | ||
if (typeof model.get(model.idAttribute) !== "undefined") { | ||
return db.find(model, options, callback); | ||
} else { | ||
return db.findAll(model, options, callback); | ||
} | ||
} | ||
}; | ||
_.extend(Backbone.Db.prototype, Backbone.Events, { | ||
@@ -183,6 +147,11 @@ save: function(cb) { | ||
var models = []; | ||
var done; | ||
var done = _.after(this.records.length, function() { | ||
queryModels(models, options, cb); | ||
}); | ||
if(this.records.length > 0) { | ||
done = _.after(this.records.length, function() { | ||
queryModels(models, options, cb); | ||
}); | ||
} else { | ||
return cb(null, []); | ||
} | ||
@@ -252,6 +221,43 @@ this.records.forEach(function(id) { | ||
return this.storage; | ||
}, | ||
sync: function(method, model, options) { | ||
options = options || {}; | ||
var self = this; | ||
var db; | ||
if(!(self instanceof Db)) { | ||
db = model.db || options.db || new Backbone.Db(model.type || model.name || model.kind || "model"); | ||
debug('using db from model'); | ||
} else { | ||
debug('using self as database'); | ||
db = self; | ||
} | ||
debug("sync %s %s",method,JSON.stringify(options)); | ||
function callback(err, res, resp) { | ||
debug('callback '+err+" "+JSON.stringify(res)); | ||
if((err && options.error) || (!err && !res && options.error)) { | ||
err = err || new Error("not found"); | ||
return options.error(err, resp); | ||
} else if(options.success && res) { | ||
debug('success %s', JSON.stringify(res)); | ||
return options.success(res, resp); | ||
} | ||
} | ||
switch (method) { | ||
case 'create': | ||
return db.create(model, options, callback); | ||
case 'update': | ||
return db.update(model, options, callback); | ||
case 'delete': | ||
return db.destroy(model, options, callback); | ||
case 'read': | ||
if (typeof model.get(model.idAttribute) !== "undefined") { | ||
return db.find(model, options, callback); | ||
} else { | ||
return db.findAll(model, options, callback); | ||
} | ||
} | ||
} | ||
}); | ||
Backbone.Db.sync = Backbone.Db.prototype.sync; | ||
module.exports = Backbone.Db; | ||
Db.sync = Db.prototype.sync; | ||
module.exports = Db; |
{ | ||
"name": "backbone-db", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Key-Value database storage interface, localStorage and in-process implementations", | ||
@@ -25,5 +25,5 @@ "main": "index.js", | ||
"backbone": "~1.1.0", | ||
"underscore": "~1.4.4", | ||
"debug": "~0.7.2", | ||
"jsonquery": "~0.1.3" | ||
"jsonquery": "~0.1.3", | ||
"lodash": "~2.4.1" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
@@ -5,3 +5,48 @@ # Backbone DB | ||
## Usage | ||
### api | ||
```js | ||
var DB = function DB(name) { | ||
this.name = name; | ||
}; | ||
/** the interface of the backbone-db exported module. | ||
* It invokes the correct DB interface methods on this or this.db | ||
* backbone-db .sync delegates CREATE, READ, UPDATE and DELETE to this interface | ||
_.extend(DB.prototype, Backbone.Events, { | ||
find: function(model, options, cb) {}, | ||
findAll: function(model, options, cb) {}, | ||
read: function(model, options, cb) {}, | ||
update: function(model, options, cb) {}, | ||
destroy: function(model, options, cb) {}, | ||
sync: function(method, model ,options) {} // sync delegates ti the methods above, should not be extended. | ||
}); | ||
**/ | ||
var store = new DB('mymodels'); | ||
var MyModel = Backbone.Model.extend({ | ||
url: function() { | ||
if(this.isNew()) { | ||
return '/mymodels'; | ||
} | ||
return '/mymodels/' + this.get(this.idAttribute); | ||
}, | ||
db: store, | ||
sync: store.sync | ||
}); | ||
var me = new MyModel({username:"Nomon"}); | ||
me.save(null, {success: function() { | ||
var me2 = new MyModel({id:me.get(me.idAttribute)}); | ||
me2.fetch({success: function(model) { | ||
console.log("My username:",model.get('username')); | ||
}}); | ||
}, error: function(err) { | ||
console.error(err); | ||
}}); | ||
// Output: My username: Nomon | ||
``` |
var Db = require('../'); | ||
var assert = require('assert'); | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
@@ -14,3 +14,4 @@ | ||
}, | ||
sync: db.sync.bind(db) | ||
db: db, | ||
sync: db.sync | ||
}); | ||
@@ -23,3 +24,4 @@ | ||
}, | ||
sync: db.sync.bind(db) | ||
db: db, | ||
sync: db.sync | ||
}); | ||
@@ -33,3 +35,3 @@ | ||
}, error: function(err) { | ||
assert(err) | ||
t(err); | ||
}}); | ||
@@ -36,0 +38,0 @@ }); |
var Db = require('../'); | ||
var assert = require('assert'); | ||
var _ = require('underscore'); | ||
var _ = require('lodash'); | ||
var Backbone = require('backbone'); | ||
@@ -5,0 +5,0 @@ |
18910
10
596
51
+ Addedlodash@~2.4.1
+ Addedlodash@2.4.2(transitive)
- Removedunderscore@~1.4.4
- Removedunderscore@1.4.4(transitive)