Permalink
Browse files

Added ModelBase base class, emit actual instance data for save/update…

… on single instances
  • Loading branch information...
1 parent 3bee8b0 commit 0804d40b2fb537340fbf6921ed6379cf7b2248f1 mde committed Oct 20, 2012
@@ -1,5 +1,6 @@
var BaseAdapter
, EventEmitter = require('events').EventEmitter
+ , model = require('../index')
, adapter = require('./index')
, utils = require('utilities');
@@ -14,7 +15,12 @@ utils.mixin(BaseAdapter.prototype, new (function () {
};
this.update = function (data, query, callback) {
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
};
this.remove = function (query, callback) {
@@ -123,7 +123,12 @@ utils.mixin(Adapter.prototype, new (function () {
_data[key][id] = item;
});
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
};
this.remove = function (query, callback) {
@@ -165,7 +170,12 @@ utils.mixin(Adapter.prototype, new (function () {
item._saved = true;
_data[key][id] = item;
});
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
this.createTable = function (names, callback) {
@@ -232,15 +232,17 @@ utils.mixin(Adapter.prototype, new (function () {
item = item.toData({whitelist: ['_id', 'id', 'createdAt']});
- collection.update({id: id}, item, function (err, data) {
+ collection.update({id: id}, item, function (err, res) {
if (err) {
callback(err, null);
}
else {
- // FIXME: What is the right data to return here? Right now this
- // is basically overwriting a doc, but we might be supporting
- // bulk-updates at some point
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
});
}
@@ -281,7 +283,6 @@ utils.mixin(Adapter.prototype, new (function () {
, items = Array.isArray(data) ? data.slice() : [data]
, collectionName = _collectionizeModelName(items[0].type)
, collection = this.client.collection(collectionName)
- , ret = []
, insert;
insert = function () {
@@ -300,13 +301,17 @@ utils.mixin(Adapter.prototype, new (function () {
item.id = id;
item._id = res._id;
item._saved = true;
- ret.push(data);
insert();
}
});
}
else {
- callback(null, ret);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
};
insert();
View
@@ -211,15 +211,17 @@ utils.mixin(Adapter.prototype, new (function () {
, data: item
};
- this.request(requestOpts, function (err, data) {
+ this.request(requestOpts, function (err, res) {
if (err) {
callback(err, null);
}
else {
- // FIXME: What is the right data to return here? Right now this
- // is basically overwriting a doc, but we might be supporting
- // bulk-updates at some point
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
});
}
@@ -305,7 +307,6 @@ utils.mixin(Adapter.prototype, new (function () {
var self = this
, items = Array.isArray(data) ? data.slice() : [data]
, bucket = _bucketizeModelName(items[0].type)
- , ret = []
, insert;
insert = function () {
@@ -331,13 +332,17 @@ utils.mixin(Adapter.prototype, new (function () {
else {
item.id = id;
item._saved = true;
- ret.push(data);
insert();
}
});
}
else {
- callback(null, ret);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
};
insert();
@@ -144,10 +144,12 @@ utils.mixin(Adapter.prototype, new (function () {
callback(err, null);
}
else {
- // FIXME: What is the right data to return here? SQL updates
- // can affect lots of rows, and I don't think we want to limit
- // it to single-item updates
- callback(null, true);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
});
};
@@ -192,10 +194,15 @@ utils.mixin(Adapter.prototype, new (function () {
rows = res.rows;
for (var i = 0, ii = items.length; i < ii; i++) {
item = items[i];
- item.id = rows[i].id;
+ item.id = rows[i].id; // Set the id from the row
item._saved = true;
}
- callback(null, items.length == 1 ? items[0] : items);
+ if (data instanceof model.ModelBase) {
+ callback(null, data);
+ }
+ else {
+ callback(null, true);
+ }
}
});
};
View
@@ -49,6 +49,7 @@ var model = {}
utils.mixin(model, new (function () {
+ this.ModelBase = function () {};
this.adapters = {};
this.loadedAdapters = {};
this.descriptionRegistry = {};
@@ -451,7 +452,7 @@ utils.mixin(model, new (function () {
}
// Data may by just a bag or params, or an actual instance
- if (typeof data.emit == 'function') {
+ if (data instanceof model.ModelBase) {
data.emit('beforeUpdate');
}
@@ -546,9 +547,11 @@ utils.mixin(model, new (function () {
// actual constructor
utils.mixin(origProto, defined);
- ModelCtor.prototype = origProto;
+ ModelCtor.prototype = new model.ModelBase();
// Add eventing to instances
utils.enhance(ModelCtor.prototype, new EventEmitter());
+ // Preserve any inherited shit from the definition proto
+ utils.enhance(ModelCtor.prototype, origProto);
model[name] = ModelCtor;
View
@@ -161,9 +161,9 @@ tests = {
});
}
-, 'emit static update': function (next) {
+, 'emit static update for single instance': function (next) {
User.once('update', function (res) {
- assert.ok(res);
+ assert.ok(res instanceof User);
next();
});
var user = User.create(_params);

0 comments on commit 0804d40

Please sign in to comment.