From bef1bc1ca4fb5fc16be5a2135f2b01526b591f32 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Tue, 28 Jan 2014 13:45:00 -0800 Subject: [PATCH] Add change / delete events --- lib/dao.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index b556e7b1..954f3052 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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); }); }); });