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