1

Is it possible to reuse / call the blueprint function (find/create/update/destory) and just add some items needed for the controllers. I'm sorry if I'm having hard time expressing my question but hopefully my example will help.

Example:

modue.exports = function(){
    index: ....,
    create: function(req, res){
      try{
        // i want to call the blueprint here to save some things
        create(req, res);
        // then do more after creating the record
        ....
      }catch(err){
       // want to catch some error here like validation err 
       // instead of sending it to   res.serverErr();
      }
    }
    ....
}
ginad
  • 1,863
  • 2
  • 27
  • 42
  • What do you want to do after calling the default `create` action? If you want to send more response data, the answer is NO. – bnuhero Mar 09 '14 at 10:39
  • Yes, see http://stackoverflow.com/questions/22273789/crud-blueprint-overriding-in-sailsjs – sgress454 Mar 09 '14 at 19:27

2 Answers2

0
//File  api/controller/UserController.js

// suppose the model is User under api/models
modue.exports =  {
     create: function(req,res){
        // pass req.query to User Model's Create function, so you dont need to rewrite all
        //optional paramters for this overwrite version 
        User.create(req.query).exec(function(e, r){
           if(e){
               // !!!try to create in a different way!
            }
        })
   }
}
0

You need to first copy blueprint folder from sails which is present in node_modules folder Paste the blueprint folder in you api folder Then in your controller for e.g UserController include actionUtil for e.g

var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');

module.exports = {
    create: function (req, res) {
        // paste code from blueprint create.js file
        var Model = actionUtil.parseModel(req);

        // Create data object (monolithic combination of all parameters)
        // Omit the blacklisted params (like JSONP callback param, etc.)
        var data = actionUtil.parseValues(req);


        // Create new instance of model using data from params
        Model.create(data).exec(function created(err, newInstance) {

            // Differentiate between waterline-originated validation errors
            // and serious underlying issues. Respond with badRequest if a
            // validation error is encountered, w/ validation info.
            if (err)
                return res.serverError({status:500, message:'error', err: err});

            // If we have the pubsub hook, use the model class's publish method
            // to notify all subscribers about the created item
            if (req._sails.hooks.pubsub) {
                if (req.isSocket) {
                    Model.subscribe(req, newInstance);
                    Model.introduce(newInstance);
                }
                // Make sure data is JSON-serializable before publishing            
                var publishData = _.isArray(newInstance) ?
                    _.map(newInstance, function (instance) {
                        return instance.toJSON();
                    }) :
                    newInstance.toJSON();
                Model.publishCreate(publishData, !req.options.mirror && req);
            }
            // do your after create stuff here
            // Send JSONP-friendly response if it's supported
            res.ok({status: 200, message: 'ok', results: newInstance});
        });


    }
}