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

Support async #with contexts #1293

Closed
wants to merge 1 commit into from
Closed

Support async #with contexts #1293

wants to merge 1 commit into from

Conversation

andreas-gruenbacher
Copy link

@andreas-gruenbacher andreas-gruenbacher commented Jan 5, 2017

In {{#with context}}body{{/with}}, when context is a function, the
functions's return value is used as the context in body. This allows
for computed context, but not when that context is provided
asynchronously, such as when querying a database.

To support asynchronous contexts as well, check if context is a function
that takes arguments. If it is, call it with the callback to invoke
when done as the first, and #with's hash arguments as the second
argument. For example, with the following context function:

 person: function(fn) {
    fn({first: 'Alan', last: 'Johnson'});
  }

The template:

  {{#with person}}{{first}} {{last}}{{/with}}

will render to:

  Alan Johnson

@andreas-gruenbacher
Copy link
Author

A similar approach would work for #each as well, of course.

In '{{#with context}}body{{/with}}', when context is a function, the
functions's return value is used as the context in body.  This allows
for computed context, but not when that context is provided
asynchronously, such as when querying a database.

To support asynchronous contexts as well, check if context is a function
that takes arguments.  If it is, call it with the callback to invoke
when done as the first, and #with's hash arguments as the second
argument.  For example, with the following context function:

  person: function(fn) {
    return fn({first: 'Alan', last: 'Johnson'});
  }

The template:

  {{#with person}}{{first}} {{last}}{{/with}}

will render to:

  Alan Johnson
@andreas-gruenbacher
Copy link
Author

This is unfortunately implemented wrong so far; need to figure out how to fix it.

@andreas-gruenbacher
Copy link
Author

I see that asynchronous helpers have been discussed before (#141), and that people were not very interested. So sorry for the noise.

@nknapp
Copy link
Collaborator

nknapp commented Jan 5, 2017 via email

@andreas-gruenbacher
Copy link
Author

@nknapp thanks. I've had a quick look at 'promised-handlebars' and the placeholder approach seems so awfully wrong that I'd much rather switch to a different template engine that supports async helpers in the long run. For now, I think I'll just resort to doing all possible database queries ahead of the rendering.

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