Pass locals into custom response functions #121

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants

Previously, it was a pain to respond to a request with JSON while using respond. The controller object is cyclical, so passing it into JSON.stringify fails. Furthermore, that doesn't filter underscores.

So, this simply passes properly-filtered locals into custom response functions. It could be used for all sorts of things that accept plain ol' JSON, but here's a simple example of responding with either HTML or JSON:

pagesController.main = function() {
  var self = this;
  this.title = 'Locomotive';
  this.respond({
    html: true,
    json: function(locals) { self.res.json(locals); } // new magic
  })
};

I've only tested manually, but I'd be happy to write up some tests!

@RaphiePS RaphiePS Pass locals into custom response functions
Especially handy for responding with JSON:

    pagesController.main = function() {
      var self = this;
      this.title = 'Locomotive';
      this.respond({
        html: true,
        json: function(locals) { self.res.json(locals); } // new magic
      })
    };
c1e8e2d

Coverage Status

Coverage increased (+0.18%) when pulling c1e8e2d on RaphiePS:master into 3925320 on jaredhanson:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment