"loaded" hook in DAO.find: ctx.data, not instance

Fix the implementation od DAO.find to provide "ctx.data" to the
"loaded" hook.
This commit is contained in:
Miroslav Bajtoš 2015-12-18 15:43:20 +01:00
parent 89e4555bc0
commit 3028329126
2 changed files with 57 additions and 48 deletions

View File

@ -1455,7 +1455,25 @@ DataAccessObject.find = function find(query, options, cb) {
async.each(data, function(item, callback) { async.each(data, function(item, callback) {
var d = item;//data[i]; var d = item;//data[i];
var Model = self.lookupModel(d); var Model = self.lookupModel(d);
var obj = new Model(d, {fields: query.fields, applySetters: false, persisted: true});
context = {
Model: Model,
data: d,
isNewInstance: false,
hookState: hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
if (err) return callback(err);
d = context.data;
var ctorOpts = {
fields: query.fields,
applySetters: false,
persisted: true
};
var obj = new Model(d, ctorOpts);
if (query && query.include) { if (query && query.include) {
if (query.collect) { if (query.collect) {
@ -1487,24 +1505,14 @@ DataAccessObject.find = function find(query, options, cb) {
delete obj.__data.__cachedRelations; delete obj.__data.__cachedRelations;
} }
} }
if (obj !== undefined) { if (obj !== undefined) {
context = {
Model: Model,
instance: obj,
isNewInstance: false,
hookState: hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
if (err) return callback(err);
results.push(obj); results.push(obj);
callback(); callback();
});
} else { } else {
callback(); callback();
} }
});
}, },
function(err) { function(err) {
if (err) return cb(err); if (err) return cb(err);

View File

@ -129,7 +129,7 @@ module.exports = function(dataSource, should) {
it('applies updates from `loaded` hook', function(done) { it('applies updates from `loaded` hook', function(done) {
TestModel.observe('loaded', pushContextAndNext(function(ctx) { TestModel.observe('loaded', pushContextAndNext(function(ctx) {
ctx.instance.extra = 'hook data'; ctx.data.extra = 'hook data';
})); }));
TestModel.find( TestModel.find(
@ -138,7 +138,7 @@ module.exports = function(dataSource, should) {
if (err) return done(err); if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({ observedContexts.should.eql(aTestModelCtx({
instance: { data: {
id: "1", id: "1",
name: "first", name: "first",
extra: "hook data" extra: "hook data"
@ -147,6 +147,8 @@ module.exports = function(dataSource, should) {
hookState: { test: true }, hookState: { test: true },
options: {} options: {}
})); }));
list[0].should.have.property('extra', 'hook data');
done(); done();
}); });
}) })
@ -1747,10 +1749,9 @@ module.exports = function(dataSource, should) {
// returns an array and NOT a single instance. // returns an array and NOT a single instance.
observedContexts.should.eql([ observedContexts.should.eql([
aTestModelCtx({ aTestModelCtx({
instance: { data: {
id: existingInstance.id, id: existingInstance.id,
name: 'first', name: 'first',
extra: null
}, },
isNewInstance: false, isNewInstance: false,
options: { notify: false } options: { notify: false }