Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sails.js

SailsJS provides default blueprint actions, such as find, update, create, etc.

I need to override some of them to suit particular business purposes. However, I would like to add to them, instead of simply override them. How to call the default blueprint actions in the overriding action.

For example, update can update the record of a model.

update 

I want to have update do more, besides updating the record of a model. In my custom update method, I do not want to duplicate default update. How can I do that?

like image 699
windchime Avatar asked Jan 28 '15 23:01

windchime


2 Answers

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

like image 141
Mindastic Avatar answered Sep 28 '22 01:09

Mindastic


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.

like image 26
Meeker Avatar answered Sep 28 '22 00:09

Meeker