Reformat notifyObserversOf context argument

This commit is contained in:
Fabien Franzen 2015-03-05 15:53:34 +01:00
parent f42859f2e5
commit 18b58558cc
2 changed files with 58 additions and 82 deletions

View File

@ -231,9 +231,8 @@ DataAccessObject.create = function (data, options, cb) {
Model = this.lookupModel(data); // data-specific Model = this.lookupModel(data); // data-specific
if (Model !== obj.constructor) obj = new Model(data); if (Model !== obj.constructor) obj = new Model(data);
Model.notifyObserversOf('before save', { var context = { Model: Model, instance: obj, hookState: hookState };
Model: Model, instance: obj, hookState: hookState Model.notifyObserversOf('before save', context, function(err) {
}, function(err) {
if (err) return cb(err); if (err) return cb(err);
data = obj.toObject(true); data = obj.toObject(true);
@ -271,9 +270,8 @@ DataAccessObject.create = function (data, options, cb) {
if (err) { if (err) {
return cb(err, obj); return cb(err, obj);
} }
Model.notifyObserversOf('after save', { var context = { Model: Model, instance: obj, hookState: hookState };
Model: Model, instance: obj, hookState: hookState Model.notifyObserversOf('after save', context, function(err) {
}, function(err) {
cb(err, obj); cb(err, obj);
if(!err) Model.emit('changed', obj); if(!err) Model.emit('changed', obj);
}); });
@ -357,9 +355,8 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
return this.create(data, options, cb); return this.create(data, options, cb);
} }
Model.notifyObserversOf('access', { var context = { Model: Model, query: byIdQuery(Model, id), hookState: hookState };
Model: Model, query: byIdQuery(Model, id), hookState: hookState Model.notifyObserversOf('access', context, doUpdateOrCreate);
}, doUpdateOrCreate);
function doUpdateOrCreate(err, ctx) { function doUpdateOrCreate(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
@ -367,7 +364,10 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
var isOriginalQuery = isWhereByGivenId(Model, ctx.query.where, id) var isOriginalQuery = isWhereByGivenId(Model, ctx.query.where, id)
if (Model.getDataSource().connector.updateOrCreate && isOriginalQuery) { if (Model.getDataSource().connector.updateOrCreate && isOriginalQuery) {
var context = { var context = {
Model: Model, where: ctx.query.where, data: data, hookState: hookState Model: Model,
where: ctx.query.where,
data: data,
hookState: hookState
}; };
Model.notifyObserversOf('before save', context, function(err, ctx) { Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
@ -404,9 +404,8 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
Model.emit('changed', inst); Model.emit('changed', inst);
} }
} else { } else {
Model.notifyObserversOf('after save', { var context = { Model: Model, instance: obj, hookState: hookState };
Model: Model, instance: obj, hookState: hookState Model.notifyObserversOf('after save', context, function(err) {
}, function(err) {
cb(err, obj); cb(err, obj);
if(!err) { if(!err) {
Model.emit('changed', inst); Model.emit('changed', inst);
@ -506,9 +505,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
} }
if (created) { if (created) {
Model.notifyObserversOf('after save', { var context = { Model: Model, instance: obj, hookState: hookState };
Model: Model, instance: obj, hookState: hookState Model.notifyObserversOf('after save', context, function(err) {
}, function(err) {
if (cb.promise) { if (cb.promise) {
cb(err, [obj, created]); cb(err, [obj, created]);
} else { } else {
@ -540,9 +538,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
this.applyScope(query); this.applyScope(query);
Model.notifyObserversOf('access', { var context = { Model: Model, query: query, hookState: hookState };
Model: Model, query: query, hookState: hookState Model.notifyObserversOf('access', context, function (err, ctx) {
}, function (err, ctx) {
if (err) return cb(err); if (err) return cb(err);
var query = ctx.query; var query = ctx.query;
@ -554,9 +551,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
Model.applyProperties(enforced, obj); Model.applyProperties(enforced, obj);
obj.setAttributes(enforced); obj.setAttributes(enforced);
Model.notifyObserversOf('before save', { var context = { Model: Model, instance: obj, hookState: hookState };
Model: Model, instance: obj, hookState: hookState Model.notifyObserversOf('before save', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
var obj = ctx.instance; var obj = ctx.instance;
@ -1122,9 +1118,8 @@ DataAccessObject.find = function find(query, options, cb) {
// using all documents // using all documents
// TODO [fabien] use default scope here? // TODO [fabien] use default scope here?
self.notifyObserversOf('access', { var context = { Model: self, query: query, hookState: hookState };
Model: self, query: query, hookState: hookState self.notifyObserversOf('access', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
self.getDataSource().connector.all(self.modelName, {}, function (err, data) { self.getDataSource().connector.all(self.modelName, {}, function (err, data) {
@ -1218,9 +1213,8 @@ DataAccessObject.find = function find(query, options, cb) {
if (options.notify === false) { if (options.notify === false) {
self.getDataSource().connector.all(self.modelName, query, allCb); self.getDataSource().connector.all(self.modelName, query, allCb);
} else { } else {
this.notifyObserversOf('access', { var context = { Model: this, query: query, hookState: hookState };
Model: this, query: query, hookState: hookState this.notifyObserversOf('access', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
var query = ctx.query; var query = ctx.query;
self.getDataSource().connector.all(self.modelName, query, allCb); self.getDataSource().connector.all(self.modelName, query, allCb);
@ -1318,6 +1312,7 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
where = query.where; where = query.where;
var hookState = {}; var hookState = {};
var context = { var context = {
Model: Model, where: whereIsEmpty(where) ? {} : where, hookState: hookState Model: Model, where: whereIsEmpty(where) ? {} : where, hookState: hookState
}; };
@ -1326,13 +1321,10 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
doDelete(where); doDelete(where);
} else { } else {
query = { where: whereIsEmpty(where) ? {} : where }; query = { where: whereIsEmpty(where) ? {} : where };
Model.notifyObserversOf('access', { var context = { Model: Model, query: query, hookState: hookState };
Model: Model, query: query, hookState: hookState Model.notifyObserversOf('access', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
var context = { var context = { Model: Model, where: ctx.query.where, hookState: hookState };
Model: Model, where: ctx.query.where, hookState: hookState
};
Model.notifyObserversOf('before delete', context, function(err, ctx) { Model.notifyObserversOf('before delete', context, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
doDelete(ctx.where); doDelete(ctx.where);
@ -1365,9 +1357,8 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
return cb(err, data); return cb(err, data);
} }
Model.notifyObserversOf('after delete', { var context = { Model: Model, where: where, hookState: hookState };
Model: Model, where: where, hookState: hookState Model.notifyObserversOf('after delete', context, function(err) {
}, function(err) {
cb(err, data); cb(err, data);
if (!err) if (!err)
Model.emit('deletedAll', whereIsEmpty(where) ? undefined : where); Model.emit('deletedAll', whereIsEmpty(where) ? undefined : where);
@ -1490,9 +1481,8 @@ DataAccessObject.count = function (where, options, cb) {
var Model = this; var Model = this;
var hookState = {}; var hookState = {};
this.notifyObserversOf('access', { var context = { Model: Model, query: { where: where }, hookState: hookState };
Model: Model, query: { where: where }, hookState: hookState this.notifyObserversOf('access', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
where = ctx.query.where; where = ctx.query.where;
Model.getDataSource().connector.count(Model.modelName, cb, where); Model.getDataSource().connector.count(Model.modelName, cb, where);
@ -1541,9 +1531,8 @@ DataAccessObject.prototype.save = function (options, cb) {
var modelName = Model.modelName; var modelName = Model.modelName;
var hookState = {}; var hookState = {};
Model.notifyObserversOf('before save', { var context = { Model: Model, instance: inst, hookState: hookState };
Model: Model, instance: inst, hookState: hookState Model.notifyObserversOf('before save', context, function(err) {
}, function(err) {
if (err) return cb(err); if (err) return cb(err);
var data = inst.toObject(true); var data = inst.toObject(true);
@ -1578,9 +1567,8 @@ DataAccessObject.prototype.save = function (options, cb) {
return cb(err, inst); return cb(err, inst);
} }
inst._initProperties(data, { persisted: true }); inst._initProperties(data, { persisted: true });
Model.notifyObserversOf('after save', { var context = { Model: Model, instance: inst, hookState: hookState };
Model: Model, instance: inst, hookState: hookState Model.notifyObserversOf('after save', context, function(err) {
}, function(err) {
if (err) return cb(err, inst); if (err) return cb(err, inst);
updateDone.call(inst, function () { updateDone.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
@ -1665,18 +1653,13 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
var Model = this; var Model = this;
var hookState = {}; var hookState = {};
Model.notifyObserversOf('access', { var context = { Model: Model, query: { where: where }, hookState: hookState };
Model: Model, query: { where: where }, hookState: hookState Model.notifyObserversOf('access', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
Model.notifyObserversOf( var context = {
'before save', Model: Model, where: ctx.query.where, data: data, hookState: hookState
{ };
Model: Model, Model.notifyObserversOf('before save', context,
where: ctx.query.where,
data: data,
hookState: hookState
},
function(err, ctx) { function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
doUpdate(ctx.where, ctx.data); doUpdate(ctx.where, ctx.data);
@ -1699,15 +1682,10 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
var connector = Model.getDataSource().connector; var connector = Model.getDataSource().connector;
connector.update(Model.modelName, where, data, function(err, count) { connector.update(Model.modelName, where, data, function(err, count) {
if (err) return cb (err); if (err) return cb (err);
Model.notifyObserversOf( var context = {
'after save', Model: Model, where: where, data: data, hookState: hookState
{ };
Model: Model, Model.notifyObserversOf('after save', context, function(err, ctx) {
where: where,
data: data,
hookState: hookState
},
function(err, ctx) {
return cb(err, count); return cb(err, count);
}); });
}); });
@ -1759,13 +1737,14 @@ DataAccessObject.prototype.remove =
var id = getIdValue(this.constructor, this); var id = getIdValue(this.constructor, this);
var hookState = {}; var hookState = {};
Model.notifyObserversOf('access', { var context = {
Model: Model, query: byIdQuery(Model, id), hookState: hookState Model: Model, query: byIdQuery(Model, id), hookState: hookState
}, function(err, ctx) { };
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
Model.notifyObserversOf('before delete',{ var context = { Model: Model, where: ctx.query.where, hookState: hookState };
Model: Model, where: ctx.query.where, hookState: hookState Model.notifyObserversOf('before delete', context, function(err, ctx) {
}, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
doDeleteInstance(ctx.where); doDeleteInstance(ctx.where);
}); });
@ -1778,9 +1757,8 @@ DataAccessObject.prototype.remove =
// We must switch to full query-based delete. // We must switch to full query-based delete.
Model.deleteAll(where, { notify: false }, function(err) { Model.deleteAll(where, { notify: false }, function(err) {
if (err) return cb(err); if (err) return cb(err);
Model.notifyObserversOf('after delete', { var context = { Model: Model, where: where, hookState: hookState };
Model: Model, where: where, hookState: hookState Model.notifyObserversOf('after delete', context, function(err) {
}, function(err) {
cb(err); cb(err);
if (!err) Model.emit('deleted', id); if (!err) Model.emit('deleted', id);
}); });
@ -1795,9 +1773,8 @@ DataAccessObject.prototype.remove =
} }
destroyed(function () { destroyed(function () {
Model.notifyObserversOf('after delete', { var context = { Model: Model, where: where, hookState: hookState };
Model: Model, where: where, hookState: hookState Model.notifyObserversOf('after delete', context, function(err) {
}, function(err) {
cb(err); cb(err);
if (!err) Model.emit('deleted', id); if (!err) Model.emit('deleted', id);
}); });
@ -1953,9 +1930,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
done.call(inst, function () { done.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
if (err) return cb(err, inst); if (err) return cb(err, inst);
Model.notifyObserversOf('after save', { var context = { Model: Model, instance: inst, hookState: hookState };
Model: Model, instance: inst, hookState: hookState Model.notifyObserversOf('after save', context, function(err) {
}, function(err) {
if(!err) Model.emit('changed', inst); if(!err) Model.emit('changed', inst);
cb(err, inst); cb(err, inst);
}); });

View File

@ -1095,7 +1095,7 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies propagates hookState from `before delete` to `after delete` hook', function(done) { it('propagates hookState from `before delete` to `after delete`', function(done) {
TestModel.observe('before delete', pushContextAndNext(function(ctx) { TestModel.observe('before delete', pushContextAndNext(function(ctx) {
ctx.hookState.foo = 'bar'; ctx.hookState.foo = 'bar';
})); }));