Skip to content

Commit

Permalink
Add new hook 'loaded'
Browse files Browse the repository at this point in the history
  • Loading branch information
PradnyaBaviskar committed Jun 18, 2015
1 parent 019705d commit 1552fc4
Show file tree
Hide file tree
Showing 2 changed files with 467 additions and 114 deletions.
298 changes: 184 additions & 114 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,31 @@ DataAccessObject.create = function (data, options, cb) {
}
obj.__persisted = true;

saveDone.call(obj, function () {
createDone.call(obj, function () {
if (err) {
return cb(err, obj);
}
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) Model.emit('changed', obj);
context = {
Model: Model,
data: val,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
saveDone.call(obj, function () {
createDone.call(obj, function () {
if (err) {
return cb(err, obj);
}
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};

Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) Model.emit('changed', obj);
});
});
});
});
Expand Down Expand Up @@ -475,33 +485,42 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
});
}
function done(err, data, info) {
var obj;
if (data && !(data instanceof Model)) {
inst._initProperties(data, { persisted: true });
obj = inst;
} else {
obj = data;
}
if (err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
var context = {
Model: Model,
data: data,
hookState: ctx.hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
var obj;
if (data && !(data instanceof Model)) {
inst._initProperties(data, { persisted: true });
obj = inst;
} else {
obj = data;
}
} else {
var context = {
Model: Model,
instance: obj,
isNewInstance: info ? info.isNewInstance : undefined,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
}
});
}
} else {
var context = {
Model: Model,
instance: obj,
isNewInstance: info ? info.isNewInstance : undefined,
hookState: hookState,
options: options
};

Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
}
});
}
});
}
});
} else {
Expand Down Expand Up @@ -586,36 +605,46 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
function _findOrCreate(query, data, currentInstance) {
var modelName = self.modelName;
function findOrCreateCallback(err, data, created) {
var obj, Model = self.lookupModel(data);
var context = {
Model: Model,
data: data,
isNewInstance: created,
hookState: hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
var obj, Model = self.lookupModel(data);

if (data) {
obj = new Model(data, {fields: query.fields, applySetters: false,
persisted: true});
}
if (data) {
obj = new Model(data, {fields: query.fields, applySetters: false,
persisted: true});
}

if (created) {
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (created) {
var context = {
Model: Model,
instance: obj,
isNewInstance: true,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (cb.promise) {
cb(err, [obj, created]);
} else {
cb(err, obj, created);
}
if (!err) Model.emit('changed', obj);
});

} else {
if (cb.promise) {
cb(err, [obj, created]);
} else {
cb(err, obj, created);
}
if (!err) Model.emit('changed', obj);
});
} else {
if (cb.promise) {
cb(err, [obj, created]);
} else {
cb(err, obj, created);
}
}
});
}

data = removeUndefined(data);
Expand Down Expand Up @@ -1329,38 +1358,50 @@ DataAccessObject.find = function find(query, options, cb) {
var Model = self.lookupModel(d);
var obj = new Model(d, {fields: query.fields, applySetters: false, persisted: true});

if (query && query.include) {
if (query.collect) {
// The collect property indicates that the query is to return the
// standalone items for a related model, not as child of the parent object
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
if (obj === null) {
obj = undefined;
}
} else {
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
// Try to normalize the include
var includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function(inc) {
var relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}
context = {
Model: Model,
instance: obj,
isNewInstance: false,
hookState: hookState,
options: options
};

// Promote the included model as a direct property
var included = obj.__cachedRelations[relationName];
if (Array.isArray(included)) {
included = new List(included, null, obj);
if (query && query.include) {
Model.notifyObserversOf('loaded', context, function(err) {
if (query.collect) {
// The collect property indicates that the query is to return the
// standalone items for a related model, not as child of the parent object
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
if (obj === null) {
obj = undefined;
}
if (included) obj.__data[relationName] = included;
});
delete obj.__data.__cachedRelations;
}
} else {
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
// Try to normalize the include
var includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function(inc) {
var relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}

// Promote the included model as a direct property
var included = obj.__cachedRelations[relationName];
if (Array.isArray(included)) {
included = new List(included, null, obj);
}
if (included) obj.__data[relationName] = included;
});
delete obj.__data.__cachedRelations;
}
});
}
if (obj !== undefined) {
results.push(obj);
Model.notifyObserversOf('loaded', context, function(err) {
results.push(obj);
});
}
}

Expand Down Expand Up @@ -1803,22 +1844,33 @@ DataAccessObject.prototype.save = function (options, cb) {
if (err) {
return cb(err, inst);
}
inst._initProperties(data, { persisted: true });

var context = {
Model: Model,
instance: inst,
data: data,
isNewInstance: result && result.isNewInstance,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (err) return cb(err, inst);
updateDone.call(inst, function () {
saveDone.call(inst, function () {
cb(err, inst);
if(!err) {
Model.emit('changed', inst);
}
Model.notifyObserversOf('loaded', context, function(err) {
inst._initProperties(data, { persisted: true });

var context = {
Model: Model,
instance: inst,
isNewInstance: result && result.isNewInstance,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if (err) return cb(err, inst);
updateDone.call(inst, function () {
saveDone.call(inst, function () {
cb(err, inst);
if(!err) {
Model.emit('changed', inst);
}
});
});
});
});
Expand Down Expand Up @@ -1956,15 +2008,24 @@ DataAccessObject.updateAll = function (where, data, options, cb) {

function updateCallback(err, info) {
if (err) return cb (err);

var context = {
Model: Model,
where: where,
data: data,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err, ctx) {
return cb(err, info);
Model.notifyObserversOf('loaded', context, function(err, ctx) {
context = {
Model: Model,
where: where,
data: data,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err, ctx) {
return cb(err, info);
});
});
}

Expand Down Expand Up @@ -2290,20 +2351,29 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
}

function updateAttributesCallback(err) {
if (!err) inst.__persisted = true;
done.call(inst, function () {
saveDone.call(inst, function () {
if (err) return cb(err, inst);
var context = {
Model: Model,
instance: inst,
isNewInstance: false,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if(!err) Model.emit('changed', inst);
cb(err, inst);
var context = {
Model: Model,
data: data,
hookState: hookState,
options: options
};
Model.notifyObserversOf('loaded', context, function(err) {
if (!err) inst.__persisted = true;
done.call(inst, function () {
saveDone.call(inst, function () {
if (err) return cb(err, inst);

var context = {
Model: Model,
instance: inst,
isNewInstance: false,
hookState: hookState,
options: options
};
Model.notifyObserversOf('after save', context, function(err) {
if(!err) Model.emit('changed', inst);
cb(err, inst);
});
});
});
});
Expand Down
Loading

0 comments on commit 1552fc4

Please sign in to comment.