Add change / delete events

This commit is contained in:
Ritchie Martori 2014-01-28 13:45:00 -08:00
parent 08fb43adb2
commit bef1bc1ca4
1 changed files with 14 additions and 1 deletions

View File

@ -126,6 +126,7 @@ DataAccessObject.create = function (data, callback) {
function modelCreated() { function modelCreated() {
if (--wait === 0) { if (--wait === 0) {
callback(gotError ? errors : null, instances); callback(gotError ? errors : null, instances);
if(!gotError) instances.forEach(Model.emit.bind('changed'));
} }
} }
} }
@ -168,6 +169,7 @@ DataAccessObject.create = function (data, callback) {
saveDone.call(obj, function () { saveDone.call(obj, function () {
createDone.call(obj, function () { createDone.call(obj, function () {
callback(err, obj); callback(err, obj);
if(!err) Model.emit('changed', obj);
}); });
}); });
}, obj); }, obj);
@ -607,6 +609,7 @@ DataAccessObject.remove =
DataAccessObject.deleteAll = DataAccessObject.deleteAll =
DataAccessObject.destroyAll = function destroyAll(where, cb) { DataAccessObject.destroyAll = function destroyAll(where, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return; if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (!cb && 'function' === typeof where) { if (!cb && 'function' === typeof where) {
cb = where; cb = where;
@ -615,6 +618,7 @@ DataAccessObject.remove =
if (!where) { if (!where) {
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) { this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data); cb && cb(err, data);
if(!err) Model.emit('deletedAll');
}.bind(this)); }.bind(this));
} else { } else {
// Support an optional where object // Support an optional where object
@ -622,6 +626,7 @@ DataAccessObject.remove =
where = this._coerce(where); where = this._coerce(where);
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) { this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data); cb && cb(err, data);
if(!err) Model.emit('deletedAll', where);
}.bind(this)); }.bind(this));
} }
}; };
@ -635,11 +640,13 @@ DataAccessObject.removeById =
DataAccessObject.deleteById = DataAccessObject.deleteById =
DataAccessObject.destroyById = function deleteById(id, cb) { DataAccessObject.destroyById = function deleteById(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return; if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
this.getDataSource().connector.destroy(this.modelName, id, function (err) { this.getDataSource().connector.destroy(this.modelName, id, function (err) {
if ('function' === typeof cb) { if ('function' === typeof cb) {
cb(err); cb(err);
} }
if(!err) Model.emit('deleted', id);
}.bind(this)); }.bind(this));
}; };
@ -684,6 +691,7 @@ setRemoting(DataAccessObject.count, {
*/ */
DataAccessObject.prototype.save = function (options, callback) { DataAccessObject.prototype.save = function (options, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) return; if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this.constructor;
if (typeof options == 'function') { if (typeof options == 'function') {
callback = options; callback = options;
@ -740,6 +748,9 @@ DataAccessObject.prototype.save = function (options, callback) {
updateDone.call(inst, function () { updateDone.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
callback(err, inst); callback(err, inst);
if(!err) {
Model.emit('changed', inst);
}
}); });
}); });
}); });
@ -811,7 +822,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
if (stillConnecting(this.getDataSource(), this, arguments)) return; if (stillConnecting(this.getDataSource(), this, arguments)) return;
var inst = this; var inst = this;
var model = this.constructor.modelName; var Model = this.constructor
var model = Model.modelName;
if (typeof data === 'function') { if (typeof data === 'function') {
cb = data; cb = data;
@ -851,6 +863,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb
done.call(inst, function () { done.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
cb(err, inst); cb(err, inst);
if(!err) Model.emit('changed', inst);
}); });
}); });
}); });