Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Mustache.js完成模板渲染后运行回调_Javascript_Jquery_Mustache - Fatal编程技术网

Javascript 如何在Mustache.js完成模板渲染后运行回调

Javascript 如何在Mustache.js完成模板渲染后运行回调,javascript,jquery,mustache,Javascript,Jquery,Mustache,一旦Mustache.js完成模板的呈现并将其插入DOM中,是否有一种干净的方法来定义/运行回调函数?例如,类似这样的内容: Mustache.render(template, viewModel, function() {...}); displayData: function () { app.renderTemplate("test", app.data, function (returnValue) { $('.result').append(returnVal

一旦Mustache.js完成模板的呈现并将其插入DOM中,是否有一种干净的方法来定义/运行回调函数?例如,类似这样的内容:

Mustache.render(template, viewModel, function() {...});
displayData: function () {
    app.renderTemplate("test", app.data, function (returnValue) {
        $('.result').append(returnValue);
    });

},


renderTemplate: function (templatefile, data, callback) {
    var tpl = 'views/' + templatefile + '.mst';
    $.ajax(tpl,
        {
            dataType: "text",
            success: function (template) {
                var rendered = Mustache.render(template, data);
                callback(rendered);
            }
        });
}
我所能想到的最好方法是计算视图模型中插入DOM的节点数,然后使用setInterval检查DOM中是否存在那么多节点。一旦他们这样做了,我就可以调用我想要的函数。这对我来说似乎效率低下,而且可能有问题,但我不知道还能做什么。

如果您也在使用jQuery,您可以使用:

$("#element").ready(function() {
  // do something when Mustache.js has finished rendering
});


js能够流式传输模板结果。该项目的文档中对此进行了描述,但没有说明。我不确定这是否是故意的,但它似乎仍然有效。这描述了
Mustache.to_html
函数的可选回调参数,该函数在每次呈现模板块时调用。也许这可以帮助解决您的问题。

与胡须无关,实际上是关于jQuery.html()

$('.your_div').html(呈现).promise().done(函数()){


}))

mustache.js'
render()
函数不需要回调,因为它是同步的。jquery的
.html()
也是同步的

如果您确实需要回调(无论出于何种原因),您可以自己编写:

var myrender = function(template, viewModel, callback)
{
    Mustache.render(template, viewModel);
    if(typeof callback === "function")
        callback();
}

// usage:
myrender(my_template, my_viewModel, function(){
    alert("I can do anything here!");
});

您可以使用以下内容:

Mustache.render(template, viewModel, function() {...});
displayData: function () {
    app.renderTemplate("test", app.data, function (returnValue) {
        $('.result').append(returnValue);
    });

},


renderTemplate: function (templatefile, data, callback) {
    var tpl = 'views/' + templatefile + '.mst';
    $.ajax(tpl,
        {
            dataType: "text",
            success: function (template) {
                var rendered = Mustache.render(template, data);
                callback(rendered);
            }
        });
}
详细操作方法可在此处找到:

您不能使用
$(选择器)。ready(…
。这只适用于DOM初始化。您是正确的,这只在元素第一次呈现时有效。不,这只适用于告知DOM已准备就绪。不适用于任何其他情况。什么是
呈现的
变量?这没有定义。@MartinBurch我假设它是
Mustache.render(…)
@MartinBurch在示例之前使用此
var rendered=“test”
。如果您正在等待Mustache.render中的内容完成渲染,并且在完成之前运行回调,该怎么办?@Jessycormier,就像我在回答中提到的-
Mustache.render()
是同步的-因此不会发生这种情况。我为没有清楚阅读您的答案而道歉:)谢谢您的提示!