11

Is there a clean way to define/run a callback function once Mustache.js has finished rendering a template and inserting it into the DOM? For example, something like this:

Mustache.render(template, viewModel, function() {...});

The best I've been able to come up with is counting the number of nodes in my view model that will be inserted into the DOM, and then using setInterval to check if that many nodes exist in the DOM. Once they do, I can then call the function I want. This seems inefficient and potentially buggy to me, but I don't know what else to do.

4 Answers 4

5

Not related to mustache, actually it is about jQuery .html().

$('.your_div').html(rendered).promise().done(function() {

// do your stuff

});

3
  • 1
    What is the rendered variable? That's not defined. Jul 29, 2016 at 22:16
  • 1
    @MartinBurch I assume it's Mustache.render(...)
    – IronAces
    Nov 11, 2016 at 11:44
  • @MartinBurch use this var rendered = "<div>test</div>"; before the example.
    – Jessy
    Mar 13, 2017 at 1:50
2

you don't need a callback for mustache.js' render() function because it is synchronous. jquery's .html() is synchronous as well.

if you really need a callback (for whatever reason) you can code it by yourself:

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!");
});
3
  • What if you're waiting for the content inside Mustache.render to finish rendering and your callback runs before it is done?
    – Jessy
    Mar 13, 2017 at 1:52
  • 1
    @Jessycormier like i mentioned in my answer - Mustache.render() is synchronous - so this won't happen.
    – low_rents
    Mar 14, 2017 at 10:12
  • I apologies for not reading your answer clearly :) Thank you for the tip!
    – Jessy
    Mar 14, 2017 at 14:19
0

If you are also using jQuery you can use:

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

Mustache.js has the ability to stream template results. This was described in the project's documentation but was removed recently. I'm not sure if this was intentional but it still appears to work. This describes an optional callback parameter of the Mustache.to_html function that is called each time a chunk of the template is rendered. Maybe this can help with your problem.

3
  • You can't use $(selector).ready(.... This only works for DOM initializing. Apr 14, 2013 at 19:34
  • You are correct, this will only work the first time the element is rendered. Apr 14, 2013 at 19:40
  • No this will only work to tell the DOM is ready. Not for anything else. Apr 14, 2013 at 21:31
0

You can use something like this:

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);
            }
        });
}

The detailed how-to can be found here: https://www.chaensel.de/mustache-js-return-rendered-result-to-callback-function/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.