Async flow for hooks

This commit is contained in:
Anatoliy Chakkaev 2011-11-20 12:36:15 +07:00
parent 3b2b57eb7b
commit c96cf9f248
2 changed files with 71 additions and 40 deletions

View File

@ -122,15 +122,17 @@ AbstractClass.create = function (data) {
}
function create() {
obj.trigger("create", function () {
obj.trigger('create', function (done) {
this._adapter().create(modelName, data, function (err, id) {
if (id) {
defineReadonlyProp(obj, 'id', id);
this.constructor.cache[id] = obj;
}
if (callback) {
callback(err, obj);
}
done.call(this, function () {
if (callback) {
callback(err, obj);
}
});
}.bind(this));
});
}
@ -221,6 +223,9 @@ AbstractClass.prototype.save = function (options, callback) {
callback = options;
options = {};
}
callback = callback || function () {};
if (!('validate' in options)) {
options.validate = true;
}
@ -238,9 +243,7 @@ AbstractClass.prototype.save = function (options, callback) {
if (options.throws) {
throw err;
}
if (callback) {
callback(err, this);
}
callback(err, this);
}
}.bind(this));
} else {
@ -248,25 +251,33 @@ AbstractClass.prototype.save = function (options, callback) {
}
function save() {
this.trigger("save", function(){
this.trigger('save', function (saveDone) {
var modelName = this.constructor.modelName;
var data = this.toObject(true);
if (this.id) {
this.trigger("update", function(){
this._adapter().save(modelName, data, function (err) {
var inst = this;
if (inst.id) {
inst.trigger('update', function (updateDone) {
inst._adapter().save(modelName, data, function (err) {
if (err) {
console.log(err);
} else {
this.constructor.call(this, data);
inst.constructor.call(inst, data);
}
if (callback) {
callback(err, this);
}
}.bind(this));
updateDone.call(inst, function () {
saveDone.call(inst, function () {
callback(err, inst);
});
});
});
});
} else {
this.constructor.create(this, callback);
}
inst.constructor.create(inst, function (err) {
saveDone.call(inst, function () {
callback(err, inst);
});
});
}
});
}
};
@ -295,10 +306,12 @@ AbstractClass.prototype.toObject = function (onlySchema) {
};
AbstractClass.prototype.destroy = function (cb) {
this.trigger("destroy", function(){
this.trigger('destroy', function (destroyed) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
delete this.constructor.cache[this.id];
cb && cb(err);
destroyed(function () {
cb && cb(err);
});
}.bind(this));
});
};
@ -326,7 +339,7 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
}.bind(this));
function update() {
this.trigger("update", function(){
this.trigger('update', function (done) {
this._adapter().updateAttributes(model, this.id, data, function (err) {
if (!err) {
Object.keys(data).forEach(function (key) {
@ -339,7 +352,9 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
});
}.bind(this));
}
cb(err);
done.call(this, function () {
cb(err);
});
}.bind(this));
});
}

View File

@ -4,27 +4,43 @@ function Hookable() {
// hookable class
};
Hookable.afterInitialize = function(){};
Hookable.beforeValidation = function(){};
Hookable.afterValidation = function(){};
Hookable.beforeSave = function(){};
Hookable.afterSave = function(){};
Hookable.beforeCreate = function(){};
Hookable.afterCreate = function(){};
Hookable.beforeUpdate = function(){};
Hookable.afterUpdate = function(){};
Hookable.beforeDestroy = function(){};
Hookable.afterDestroy = function(){};
Hookable.afterInitialize = null;
Hookable.beforeValidation = null;
Hookable.afterValidation = null;
Hookable.beforeSave = null;
Hookable.afterSave = null;
Hookable.beforeCreate = null;
Hookable.afterCreate = null;
Hookable.beforeUpdate = null;
Hookable.afterUpdate = null;
Hookable.beforeDestroy = null;
Hookable.afterDestroy = null;
Hookable.prototype.trigger = function (action, work){
Hookable.prototype.trigger = function trigger(actionName, work) {
var capitalizedName = capitalize(actionName);
var afterHook = this.constructor["after" + capitalizedName];
var beforeHook = this.constructor["before" + capitalizedName];
var inst = this;
// we only call "before" hook when we have actual action (work) to perform
if (work) {
bHook = this.constructor["before" + capitalize(action)];
if (bHook) bHook.call(this);
work.call(this);
if (beforeHook) {
// before hook should be called on instance with one param: callback
beforeHook.call(inst, function () {
// actual action also have one param: callback
work.call(inst, next);
});
} else {
work.call(inst, next);
}
} else {
next();
}
function next(done) {
if (afterHook) afterHook.call(inst, done);
else if (done) done.call(this);
}
aHook = this.constructor["after" + capitalize(action)];
if (aHook) aHook.call(this);
};
function capitalize(string) {