Override blueprint actions

799 views
Skip to first unread message

Cosmin Molea

unread,
Mar 28, 2014, 6:14:46 AM3/28/14
to sai...@googlegroups.com
Hi,

I'm relatively new to Sails and I use it as a back-end API for an Angular app. I like a lot the 'out of the box' blueprint actions because they work great with Angular "$resource". But it turns out I need to do some extra work (call a web service with "http.request") before or after running the blueprint methods. I already tried to find a solution, but none seems to work for me.

I don't want to use lifecycle callbacks on the model because my purpose is not to modify the attributes of the object. Also, declaring "var http = require('http');" in the model seems like a no-go for me.

I also found this thread on Stack Overflow http://stackoverflow.com/questions/18110262/calling-super-method-in-sails-js-controller and I tried calling "next()" as Mike suggested, but it didn't work. I also don't want to use policies because it doesn't seem the right thing to do. IMO this stuff should be done in the controller.

module.exports = {

    delete: function (req, res, next) {

        // Call some web service before deleting the object

        next();
    },

.....

The solution I found is not nice and thus I want to do it better. For now, I've create another method in the controller, say "callWebService", which I call on the client side once the blueprint call is done. That's ugly, right? So I'm using Sails 0.9.4, but I'm wondering whether in version 0.10 it's perhaps easier to do this.

Thank you all for your help!




Cosmin Molea

unread,
Mar 28, 2014, 6:19:54 AM3/28/14
to sai...@googlegroups.com
In fact, the method I tried to override is "destroy", not "delete" (it goes the same for the "create" and "update).

module.exports = {

    destroy: function (req, res, next) {

        // Call some web service before deleting the object

        next();
    },

.....

Thanks again!

Hrvoje Erjavec

unread,
Nov 12, 2014, 9:28:20 AM11/12/14
to sai...@googlegroups.com
Hi Cosmin,

I had the same issue and it seems that there isn't a pretty way to handle this. One way to deal with the problem is to require the native sails blueprint function which is called on create: blueprintCreate(req, res).
This way it is possible to override the create function and still continue to use blueprints by calling blueprintCreate. The function is triggered after policies and before validation.

//HelloWorldController
var blueprintCreate;

blueprintCreate = require("../../node_modules/sails/lib/hooks/blueprints/actions/create");

module.exports = {
  create: function(req, res) {
    sails.log.verbose("Hello world!!!");
    return blueprintCreate(req, res);
  }
};


mot xu

unread,
Mar 25, 2016, 1:01:09 AM3/25/16
to sails.js
tks :D

Vào 21:28:20 UTC+7 Thứ Tư, ngày 12 tháng 11 năm 2014, Hrvoje Erjavec đã viết:
Reply all
Reply to author
Forward
0 new messages