Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass locals into custom response functions #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RaphiePS
Copy link

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!

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
      })
    };
@coveralls
Copy link

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants