how to call default blueprint actions in a custom overridden one?

拥有回忆 提交于 2019-12-03 06:52:37

copy everything from

PROJECT-ROOT\node_modules\sails\lib\hooks\blueprints\actions

to

PROJECT-ROOT\config\blueprints

Make sure findOne.js is lowercase. You will need to modify each one to make reference to the location of actionUtil.js. You can now modify these at your hearts content without re-inventing the wheel.

I just came across the same issue and found a different way to fix it. It may help in the future if someone has the same problem. What I finally did was to re-write the action in the controller, in my case it was add and then, after doing some stuff inside, called the default blueprint's action. So, my code looks like below:

add: function (req, res) {
    if (xxx) {
        // I need to do something only when the condition above is met
        Section.count({xxx: xxx)}).exec(function (error, count) {
            if (error) {
                return res.json(500, {error: 'There was an error while trying to run query'});
            }
            //I do what I have to do
            return sails.hooks.blueprints.middleware.add(req, res);
        });
    } else {
        //I just return the default blueprint's action
        return sails.hooks.blueprints.middleware.add(req, res);
    }
}

So, basically, default blueprint functions are stored in: sails.hooks.blueprints.middleware

You should look at lifecycle callbacks in sailsjs. So for example, you can use beforeUpdate or beforeCreate lifecycle callback to do more in the model:

var bcrypt = require('bcrypt');

module.exports = {

  attributes: {

    username: {
      type: 'string',
      required: true
    },

    password: {
      type: 'string',
      minLength: 6,
      required: true,
      columnName: 'encrypted_password'
    }

  },


  // Lifecycle Callbacks
  beforeCreate: function (values, cb) {

    // Encrypt password
    bcrypt.hash(values.password, 10, function(err, hash) {
      if(err) return cb(err);
      values.password = hash;
      //calling cb() with an argument returns an error. Useful for canceling the entire operation if some criteria fails.
      cb();
    });
  }
};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!