From 8dafb424ed5b6ec2f54f63862f44898d4d838c1b Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Mon, 9 Apr 2012 20:24:35 +0400 Subject: [PATCH] Not strict equal for scoped find, trigger beforeUpdate with data --- lib/abstract-class.js | 12 +++++++++--- lib/hookable.js | 4 ++-- lib/validatable.js | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 3bef6441..a5237812 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -441,7 +441,7 @@ AbstractClass.prototype.save = function (options, callback) { }); }); }); - }); + }, data); } else { inst.constructor.create(inst, function (err) { saveDone.call(inst, function () { @@ -577,11 +577,17 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) { }); }); }); - }); + }, data); }); } }; +AbstractClass.prototype.fromObject = function (obj) { + Object.keys(obj).forEach(function (key) { + this[key] = obj[key]; + }.bind(this)); +}; + /** * Checks is property changed based on current property and initial value * @@ -652,7 +658,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) { function find(id, cb) { anotherClass.find(id, function (err, inst) { if (err) return cb(err); - if (inst[fk] === this.id) { + if (inst[fk] == this.id) { cb(null, inst); } else { cb(new Error('Permission denied')); diff --git a/lib/hookable.js b/lib/hookable.js index fabaea1f..907fc2f9 100644 --- a/lib/hookable.js +++ b/lib/hookable.js @@ -16,7 +16,7 @@ Hookable.afterUpdate = null; Hookable.beforeDestroy = null; Hookable.afterDestroy = null; -Hookable.prototype.trigger = function trigger(actionName, work) { +Hookable.prototype.trigger = function trigger(actionName, work, data) { var capitalizedName = capitalize(actionName); var afterHook = this.constructor["after" + capitalizedName]; var beforeHook = this.constructor["before" + capitalizedName]; @@ -29,7 +29,7 @@ Hookable.prototype.trigger = function trigger(actionName, work) { beforeHook.call(inst, function () { // actual action also have one param: callback work.call(inst, next); - }); + }, data); } else { work.call(inst, next); } diff --git a/lib/validatable.js b/lib/validatable.js index 3178903d..8a81bd9f 100644 --- a/lib/validatable.js +++ b/lib/validatable.js @@ -287,7 +287,8 @@ function getConfigurator(name, opts) { * Each validation can add errors to `obj.errors` collection. * If collection is not blank, validation failed. * - * @warning This method can be called as sync only when no async validation configured. It's strongly recommended to run all validations as asyncronous. + * @warning This method can be called as sync only when no async validation + * configured. It's strongly recommended to run all validations as asyncronous. * * @param {Function} callback called with (valid) * @return {Boolean} true if no async validation configured and all passed