jaysylvester / citizen

Node.js MVC web application framework. Includes routing, serving, caching, session management, and other helpful tools.
MIT License
100 stars 7 forks source link

Add an include controller's public context to its parent controller's public context for JSON output #91

Closed jaysylvester closed 3 years ago

jaysylvester commented 3 years ago

Rendered includes are added to the calling controller's public context for HTML output like so:

const handler = () => {
  return {
    content: {
      foo: 'bar'
    },
    include: {
      _head: {
        controller: '_head'
      }
    }
  }
}

HTML view (Handlebars in this case):

<html>
  {{include._head}}
  <body>
    <p>{{foo}}</p>
  </body>
</html>

But when JSON is requested, only the parent controller's public context is included. The JSON output of the above example would be:

{
  foo: 'bar'
}

Any public context generated by the _head include is ignored. It might make more sense to extend the parent controller's public context with the include's public context when JSON is requested:

{
  foo: 'bar',
  include: {
    _head: {
      title: 'Page title',
      metaDescription: 'Page description',
      keywords: 'key, words'
    }
  }
}
jaysylvester commented 3 years ago

The entire public context is now returned in all scenarios, providing include context in all controllers, including the layout controller. Also did some other cleanup to make the chain and post-request context clearer.