是否Handlebars.registerHelper支持异步功能呢? [英] Does Handlebars.registerHelper support async functions?

查看:361
本文介绍了是否Handlebars.registerHelper支持异步功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能创建一个把手辅助函数返回异步结果呢?
也许把手支持某种形式的双向绑定?
我想使用JSONP请求JSON数据,以便jQuery.ajax也不支持异步:假,但然而这不会是我期待的完全一样的。

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.

推荐答案

您可以打电话给你的助手要什么功能,但不会给你带来任何好的。问题是,把手佣工字符串和其他人将这些字符串转换为DOM元素工作。但是,在时间的AJAX调用完成后,不会有这都是基于字符串把手生产,可以在一个辅助内成功回调捕获任何情况下建造的DOM元素之间的任何有用的连接。

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. 助手功能,可以生成HTML与所需的结构,你可能有数据的组合 - * 属性的结构。

  2. 一些JavaScript代码的它已经DOMified后作用于HTML的。这个JavaScript可以寻找的东西与数据 - * 1 属性,推出相应的AJAX调用,然后修补的DOM中签处理程序。

  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.

这篇关于是否Handlebars.registerHelper支持异步功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆