I'm trying to override the update() and create() actions in a particular controller of my Sails.js application.
What I'm trying to do is to upload a file first, then store the file path (which is stored on s3 using skipper) in the request body and then call the blueprint "create()" or "update()" action to resume the normal workflow.
I saw the the use of next() in these cases is not recommended so I would like to know what's the best approach to use here.
update: function(req, res) {
var options = {
adapter: require('skipper-s3'),
key: sails.config.s3.accessKeyId,
secret: sails.config.s3.secretAccessKey,
bucket: sails.config.s3.bucket,
region: sails.config.s3.region
};
req.file('image').upload(options, function(err, files) {
if (err) res.error(err);
_.each(files, function(file){
req.body['logo'] = file.extra.Location;
});
// CALL PARENT HERE
});
},
There isn't any way provided by Sails for you to call default blueprint action code from within a controller. However, there is a way for you to easily override the blueprint code. Simply place your own update.js file in the api/blueprints.js folder (you may have to create the folder), using the core update.js source code as a starting point.
Of course, this will override the blueprint for all models. If you just want to override it for a single model, you'll have to do so inside the controller as in your example. In this case, there would actually be nothing wrong with using next() except for the fact that in this case, it won't work. The default blueprint routes aren't bound to URLs for which there is already a custom action of the same name. If you call next(), you'll get a 404.
If you want to override the blueprint for some, but not all models, you would put the shared code in a service, and call the service code from the individual update actions in each controller.
You can end your update controller action with:
res.redirect('/yourControllerName/create');
If you want to pass any parameters along, you can pass it directly along:
res.redirect('/yourControllerName/create?param1=value1¶m2=value2');
Or you can store what you need in req.session and grab it in your create action, then clear it (I don't know if this is good practice but does work).