Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRUD blueprint overriding in sails.js

According to this closed issue in sails: https://github.com/balderdashy/sails/issues/835

CRUD Blueprint Overrides "absolutely, this is coming in v0.10"

I'd like to modify the blueprints in my sailsjs service to allow named roots (consuming in ember). Currently I'm having to customize every controller I create with actions that are largely duplicates of what is already in the blueprints.

I suspect that I can move this code out of my controllers now and into a blueprints override area, but I'm not clear on where to put that code.

Any examples or even just a pointer to the relevant code in sails the .10 repo would be greatly appreciated.

like image 910
davepreston Avatar asked Mar 08 '14 19:03

davepreston


3 Answers

Update

In order to override blueprints in Sails 1.0 in the manner described below, you must first install the "custom blueprints" plugin for your project (npm install sails-hook-custom-blueprints).


To override blueprints in Sails v0.10, you create an api/blueprints folder and add your blueprint files (e.g. find.js, create.js, etc.) within. You can take a look at the code for the default actions in the Sails blueprints hook for a head start.

Adding custom blueprints is also supported, but they currently do not get bound to routes automatically. If you create a /blueprints/foo.js file, you can bind a route to it in your /config/routes.js file with (for example):

'GET /myRoute': {blueprint: 'foo'}
like image 62
sgress454 Avatar answered Oct 20 '22 20:10

sgress454


you can add actions with these names inside your controller to override default behaviour

to change destroy behavior

module.exports = {
  destroy: function(req,res){
    Goal.update({ id: req.param('id') }, { deleted: true })
    .exec(function (err, goal) {
            if (err) return res.json(err, 400);
            return res.json(goal[0]);
     });
  }
}
like image 35
Nour Sammour Avatar answered Oct 20 '22 21:10

Nour Sammour


It is possible to use the build in blueprints, but with policies running first. These policies might verify that the user is logged in, has the correct access, or similar. Really handy!

On each model, you have available callbacks both before and after data has been stored. Dig in: http://sailsjs.com/documentation/concepts/models-and-orm/lifecycle-callbacks

There is no default callback available for blueprints result. But don't give up. It is still possible to use the build in blueprints, and only modify the output. It might not be the most elegant solution, but it works well. Check out my “hack” here: Sails blueprints lifecycle

like image 1
qualbeen Avatar answered Oct 20 '22 20:10

qualbeen