Hookable validations without breaking functionality
This commit is contained in:
parent
dd1e54ffec
commit
894a924488
|
@ -111,14 +111,12 @@ AbstractClass.create = function (data) {
|
|||
obj = new this(data);
|
||||
|
||||
// validation required
|
||||
obj.trigger("validation", function(){
|
||||
if (!obj.isValid()) {
|
||||
return callback(new Error('Validation error'), obj);
|
||||
}
|
||||
});
|
||||
if (!obj.isValid()) {
|
||||
return callback(new Error('Validation error'), obj);
|
||||
}
|
||||
}
|
||||
|
||||
obj.trigger("create", function(){
|
||||
obj.trigger("create", function () {
|
||||
this._adapter().create(modelName, data, function (err, id) {
|
||||
if (id) {
|
||||
defineReadonlyProp(obj, 'id', id);
|
||||
|
@ -220,15 +218,14 @@ AbstractClass.prototype.save = function (options, callback) {
|
|||
if (!('throws' in options)) {
|
||||
options.throws = false;
|
||||
}
|
||||
this.trigger("validation", function(){
|
||||
if (options.validate && !this.isValid()) {
|
||||
var err = new Error('Validation error');
|
||||
if (options.throws) {
|
||||
throw err;
|
||||
}
|
||||
return callback && callback(err);
|
||||
|
||||
if (options.validate && !this.isValid()) {
|
||||
var err = new Error('Validation error');
|
||||
if (options.throws) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
return callback && callback(err);
|
||||
}
|
||||
|
||||
this.trigger("save", function(){
|
||||
var modelName = this.constructor.modelName;
|
||||
|
@ -295,12 +292,11 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) {
|
|||
Object.keys(data).forEach(function (key) {
|
||||
this[key] = data[key];
|
||||
}.bind(this));
|
||||
this.trigger("validation", function(){
|
||||
if (!this.isValid()) {
|
||||
var err = new Error('Validation error');
|
||||
return cb && cb(err);
|
||||
}
|
||||
});
|
||||
|
||||
if (!this.isValid()) {
|
||||
var err = new Error('Validation error');
|
||||
return cb && cb(err);
|
||||
}
|
||||
|
||||
this.trigger("update", function(){
|
||||
this._adapter().updateAttributes(model, this.id, data, function (err) {
|
||||
|
|
|
@ -16,17 +16,17 @@ Hookable.afterUpdate = function(){};
|
|||
Hookable.beforeDestroy = function(){};
|
||||
Hookable.afterDestroy = function(){};
|
||||
|
||||
Hookable.prototype.trigger = function(action, work){
|
||||
if(work){
|
||||
bHook = this.constructor["before" + capitalize(action)];
|
||||
if(bHook) bHook.call( this );
|
||||
Hookable.prototype.trigger = function (action, work){
|
||||
if (work) {
|
||||
bHook = this.constructor["before" + capitalize(action)];
|
||||
if (bHook) bHook.call(this);
|
||||
|
||||
work.call(this)
|
||||
}
|
||||
aHook = this.constructor["after" + capitalize(action)];
|
||||
if(aHook) aHook.call( this );
|
||||
work.call(this);
|
||||
}
|
||||
aHook = this.constructor["after" + capitalize(action)];
|
||||
if (aHook) aHook.call(this);
|
||||
};
|
||||
|
||||
function capitalize(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
||||
|
||||
function capitalize(string){
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
}
|
|
@ -102,10 +102,12 @@ Validatable.prototype.isValid = function () {
|
|||
value: new Errors
|
||||
});
|
||||
|
||||
this.constructor._validations.forEach(function (v) {
|
||||
if (validationFailed(inst, v)) {
|
||||
valid = false;
|
||||
}
|
||||
this.trigger('validation', function () {
|
||||
this.constructor._validations.forEach(function (v) {
|
||||
if (validationFailed(inst, v)) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
if (valid) cleanErrors(this);
|
||||
return valid;
|
||||
|
|
Loading…
Reference in New Issue