backbone-db
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -104,8 +104,25 @@ var Backbone = require('backbone'); | ||
} | ||
function store(model) { | ||
var key = self._getKey(model, options); | ||
self.store().setItem(key, JSON.stringify(model), function(err, res) { | ||
self.records.push(model.get(model.idAttribute)); | ||
self.save(function() {}); | ||
return cb(err, model.toJSON(), res); | ||
if(model.collection) { | ||
var colKey = self._getKey(model.collection, options); | ||
self.store().getItem(colKey, function(err, res) { | ||
res = res && JSON.parse(res); | ||
res = res || []; | ||
res.push(model.get(model.idAttribute)); | ||
self.store().setItem(colKey, JSON.stringify(res), function(err) { | ||
self.records.push(model.get(model.idAttribute)); | ||
self.save(function(err) { | ||
return cb(err, model.toJSON(), res); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
self.records.push(model.get(model.idAttribute)); | ||
self.save(function(err) { | ||
return cb(err, model.toJSON(), res); | ||
}); | ||
} | ||
}); | ||
@@ -130,10 +147,18 @@ } | ||
} | ||
data = data && JSON.parse(data); | ||
if(!Array.isArray(data) || data.length == 0) { | ||
return cb(null, data || []); | ||
} | ||
var done = _.after(data.length, function(m) { | ||
return cb(null,m); | ||
}); | ||
data = data && JSON.parse(data); | ||
data = data.map(function(id) { | ||
data = self.store().getItem(self.name+':'+id, function(err, data) { | ||
return JSON.parse(data); | ||
data.forEach(function(id) { | ||
var m = new model.model({id:id}); | ||
var key = self._getKey(m, options); | ||
self.store().getItem(key, function(err, data) { | ||
data = data && JSON.parse(data); | ||
done(data); | ||
}); | ||
}); | ||
return(err, data); | ||
}); | ||
@@ -140,0 +165,0 @@ }, |
{ | ||
"name": "backbone-db", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Key-Value database storage interface, localStorage and in-process implementations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,5 +40,13 @@ var Db = require('../'); | ||
var m = new MyCollection(); | ||
assert(m.length === 0); | ||
m.create({"test":1},{success: function(model) { | ||
assert(model.get("test") === 1); | ||
t(); | ||
m.fetch({success: function() { | ||
assert(m.length === 1); | ||
t(); | ||
}, error: function(err){ | ||
assert(err); | ||
}}); | ||
}, error: function(err) { | ||
@@ -45,0 +53,0 @@ assert(err) |
Sorry, the diff of this file is not supported yet
112850
2607