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