mphasize/sails-generate-ember-blueprints

After policy

alexjp opened this issue · 6 comments

Hi,

I am trying to make use of the blueprints and have 2 hooks, for before and after create.
I guess before can be managed by policies, but how to do an after create ( before sending request ) hook ?

thanks

I originally had such a hook in the blueprints, but it turned out that I would just do a lot of evil things with it, so I removed it.
I guess you could modify the blueprints, or add other custom hooks or look into using responses.

I would be curious what your use case is going to be.

Here's the commit that removed it: 5e02619

mphasize : i have a model that when i create it, i have to do alot of log and history inserts.

for example: after create -> need to set initial status, need to create a notification, need to create the relationship in an M:N table....

One thing that i tried was adding a before create and after create callbacks into the blueprints.
Before i was just copying the whole blueprint and changing accordingly, but that is bad, for example, when you update the blueprints or fix something.

Also , i cannot do this on the model hooks, because i need req context :(

I think I was in a similar situation. What I did eventually was to create a policy that adds all the data I need to the data passed into beforeCreate and afterCreate, so that I wouldn't need the req object anymore. Maybe that can help you?

Hum, probably yes, but seems overloading the data info.

i was thinking something along this:

var blueprint = require( '../blueprints/create' );

module.exports = {
  create: function( req, res ) {
    blueprint.createRecord( req, res, this.before, this.after );
  },

  beforeCreate: function( req, res, Model, data ) {},
  afterCreate: function( req, res, Model, data, newInstance ) {}
};

in the controller.
Being a newbie in JS ( especially javascript in sails, i seem to get along fine with es6 promises in ember ), i wasnt able to manage promises and async correctly with without callbacks in the blueprints.

Could something like that be done ? if calling with a before and after callbacks, to execute them ?

Well closing this.
i think there is the solutions by policy passing the req into the data, or creating services calling the blueprint code, or copy/pasting the blueprint.

@mphasize thanks for the comments.