【问题标题】:Using underscore.js, is there a way to determine when a template has finished rendering使用 underscore.js,有没有办法确定模板何时完成渲染
【发布时间】:2013-06-21 19:28:12
【问题描述】:

我正在处理一个项目,我们正在使用 underscore.js 进行模板。有没有办法确定模板何时完成渲染,以便我可以提示另一个函数调用?

这个项目包括 jQuery,但不包括 Backbone.js,如果这对你的答案有帮助的话。

谢谢! 迈克

【问题讨论】:

  • 好吧,_.template 返回一个对模板进行实际评估的函数。该函数将结果作为字符串返回。所以每当函数返回时,渲染就完成了,它不是异步的。只需将函数调用放在渲染调用之后。还是我错过了什么?
  • 我想这就是我想要弄清楚的,如果它是异步的。这可能回答了这个问题。谢谢菲利克斯!
  • 好的 :) 添加它作为答案。

标签: javascript jquery underscore.js template-engine


【解决方案1】:

_.template 返回一个对模板进行实际评估的函数。该函数将结果作为字符串返回。所以每当函数返回时,渲染就完成了,它不是异步的。

文档中的示例:

var compiled = _.template("hello: <%= name %>");
compiled({name : 'moe'});
=> "hello: moe"

所以你可以简单地将下一个函数调用放在渲染调用之后:

var compiled = _.template("hello: <%= name %>");
var result = compiled({name : 'moe'});
someOtherFunction();

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 2018-07-30
    • 2012-04-28
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2014-07-06
    相关资源
    最近更新 更多