backbone-db
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -96,2 +96,11 @@ var Backbone = require('backbone'); | ||
// convert model's id | ||
var getId = function(model) { | ||
var id = model.get(model.idAttribute); | ||
if (_.isObject(id)) { | ||
return id.toString(); | ||
} | ||
return id; | ||
}; | ||
var Db = Backbone.Db = function Db(name) { | ||
@@ -128,4 +137,5 @@ var self = this; | ||
function store(model) { | ||
self.store().setItem(model.get(model.idAttribute), JSON.stringify(model), function (err, res) { | ||
self.records.push(model.get(model.idAttribute)); | ||
var id = getId(model); | ||
self.store().setItem(id, JSON.stringify(model), function (err, res) { | ||
self.records.push(id); | ||
self.save(function (err) { | ||
@@ -140,3 +150,4 @@ return cb(err, model.toJSON(), res); | ||
debug("FIND: "+JSON.stringify(model)); | ||
this.store().getItem(model.get(model.idAttribute), function(err, data) { | ||
var id = getId(model); | ||
this.store().getItem(id, function(err, data) { | ||
data = data && JSON.parse(data); | ||
@@ -176,5 +187,6 @@ return cb(data ? null : new Error(), data); | ||
} | ||
this.store().removeItem(model.get(model.idAttribute), function () { | ||
self.records = _.reject(self.records, function (id) { | ||
return id == model.get(model.idAttribute); | ||
var id = getId(model); | ||
this.store().removeItem(id, function () { | ||
self.records = _.reject(self.records, function (_id) { | ||
return _id == id; | ||
}); | ||
@@ -197,6 +209,7 @@ self.save(function (err) { | ||
} | ||
this.store().setItem(model.get(model.idAttribute), JSON.stringify(model), function (err, res) { | ||
var id = getId(model); | ||
this.store().setItem(id, JSON.stringify(model), function (err, res) { | ||
// if models created with id. | ||
if (self.records.indexOf(model.get(model.idAttribute)) === -1) { | ||
self.records.push(model.get(model.idAttribute)); | ||
if (self.records.indexOf(id) === -1) { | ||
self.records.push(getId(model)); | ||
} | ||
@@ -220,3 +233,3 @@ cb(err, model.toJSON(), res); | ||
var amount = options.inc.amount; | ||
var key = model.get(model.idAttribute); | ||
var key = getId(model); | ||
this.store().getItem(key, function (err, data) { | ||
@@ -269,3 +282,3 @@ data = JSON.parse(data); | ||
case 'read': | ||
if (typeof model.get(model.idAttribute) !== "undefined") { | ||
if (typeof getId(model) !== "undefined") { | ||
return db.find(model, options, callback); | ||
@@ -272,0 +285,0 @@ } else { |
{ | ||
"name": "backbone-db", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Key-Value database storage interface, localStorage and in-process implementations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -84,5 +84,12 @@ var Db = require('../'); | ||
it('should override id creation function', function(t) { | ||
var Id = function(name) { | ||
this.name = name; | ||
}; | ||
Id.prototype.toString = function() { | ||
return this.name; | ||
}; | ||
var ABCModel = MyModel.extend({ | ||
createId: function(callback) { | ||
callback(null, 'abc'); | ||
callback(null, new Id('abc')); | ||
} | ||
@@ -93,2 +100,3 @@ }); | ||
success: function() { | ||
assert(typeof m.id === 'object'); | ||
assert.equal(m.id, 'abc'); | ||
@@ -95,0 +103,0 @@ t(); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19862
638
0