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

The callback interface is not always suitable #65

Open
thany opened this issue Feb 6, 2013 · 7 comments
Open

The callback interface is not always suitable #65

thany opened this issue Feb 6, 2013 · 7 comments

Comments

@thany
Copy link

thany commented Feb 6, 2013

It needs an option to return the result of a template. I'm currently working with another plugin that has a callback of its own, and in that callback I must return a jQuery object or a blob of html for it to further process. That mean I have to immediately return something, I cannot tell it to wait for an arbitrary second callback.

Currently I have to hack my way around this, which is FUGLY, and since a template usually does nothing that's asynchronous, having the render-function return the template result would be most preferable.

@thany
Copy link
Author

thany commented Feb 6, 2013

This sort of works:

var frag = document.createDocumentFragment();
dust.render("listing-template", item, function(err, out) {
  $(frag).append(out);
});
return frag;

When some callback requires that content is returned. See why I said "hack my way around"? This is ugly and feels like a nice oldskool hack, which is not what I want in production code. It is a good thing that in this particular case the callback doesn't require a string to be returned, and I could get away with a doc fragment.

@brianblocker
Copy link

I believe that this may have something to do with Node compatibility, since the author appears to be following nodes "non-blocking" convention of an err as the first param of the callback. Also the asynchronous nature of it may play a part. Would definitely love to see an option for this though.

@thany
Copy link
Author

thany commented Feb 14, 2013

Why not both?

@jimmyhchan
Copy link

Pre-compile the templates and include it in your page (as a script include or whatever dependency manager) before you call dust.render. You know it is available if dust.cache['myTemplateName'] is exists.

This issue was discussed very recently at https:///github.com/linkedin/dustjs/.

@thany
Copy link
Author

thany commented Jun 7, 2013

Precompilation is not nearly always an option. I have to deal with archaic CMS systems, and there's just no way that precompiling is ever going to be part of their build-process. Don't ask, it's just the way it is. Manually performing another step in the build process is asking for problems, so no, precompilation isn't going to cut the mustard.

@jimmyhchan
Copy link

You'll want to look into async management Something with Promise or react to events that are fired.
jQuery deferreds may be what you want.

There was some talk in the LinkedIn fork to allow users to require sync render even if it has to fetch and compile the code client side. It's not performant in many ways and client-side async management libraries exist, so it got very deprioritized.

@sethkinast
Copy link

So compiling is always synch. That's safe.

You're also safe to do something like this:

var str;
dust.render(tmpl, ctx, function(err, out) { str = out; });
return str; // str is populated

UNLESS you do one of the following things:

  • Load a partial asynchronously (via AJAX e.g.)
  • Call dust.stream
  • Write a context helper that uses chunk.map in an async manner

We'd like Dust to return Promises but it's not worth adding a dependency. Someday Promises will be native in all browsers and we'll handle it then. Until then, you can wire up your own Promise and resolve it in the callback, or use the pseudosync behavior above (which releases Zalgo but eh).

eugeneai pushed a commit to eugeneai/dustjs that referenced this issue Dec 9, 2015
literal params relaxed to allow eol
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

No branches or pull requests

4 participants