如何在Sails框架中运行beforeValidate仅用于更新操作?

时间:2015-07-31 09:36:28

标签: sails.js

我想知道在Sails框架中触发beforeValidate时的当前操作,请查看我的代码:

module.exports = {
    attributes: {
        ...
        log: 'string'
    },
    beforeValidate: function(values, cb) {

        // I want to know the action is create new record or update current record
        // If action is update, how to get primary key of record will be updated

        /*
        if (isCreateNewRecord) {
            values.log = 'New created...'; // for example
        } else {
            values.log = 'Update record ID: ' + recordID;
        }
        */

        cb();
    }
}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

嗯,你不应该这样做......这就是为什么我们有beforeCreatebeforeUpdate

beforeCreate: function (values, cb) {
  values.log = 'New created...';

  return cb();
},

beforeUpdate: function (valuesToUpdate, cb) {
  valuesToUpdate.log = 'Update record ID: ' + valuesToUpdate.id;

  return cb();
}

查看the docs