Does Handlebars.registerHelper support async functions?

点点圈 提交于 2020-01-04 17:29:09

问题


Is it possible to create a Handlebars Helper Function that returns async result? Maybe Handlebars supports some kind of two way binding? I want to use JSON data from JSONP request so jQuery.ajax also does not support async: false, but however this would not be exactly the same that I am looking for.


回答1:


You can call whatever functions you want in a helper but that won't do you any good. The problem is that Handlebars helpers work with strings and someone else will convert those strings to DOM elements. But, by the time your AJAX call finishes, there won't be any useful connection between the DOM elements that were built based on the string that Handlebars produced and any context that you can capture in a success callback inside a helper.

I think the best you can do is break it into two pieces:

  1. The helper function can produce HTML with a desired structure, you'd probably have some combination of class and data-* attributes for the structure.
  2. Some JavaScript that acts on the HTML after it has been DOMified. This JavaScript could look for things with the class and data-* attributes from 1, launch the appropriate AJAX calls, and then patch up the DOM in the success handlers.

Maybe not that helpful but that's probably the best you can do when working with a text-based template system.



来源:https://stackoverflow.com/questions/18904474/does-handlebars-registerhelper-support-async-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!