Answer the question
In order to leave comments, you need to log in
How to catch html build event based on handlebars template?
Using Ajax, I load a certain set of data from the server, then I build html according to the handlebars template using this data and display it to the user:
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
success: function(msg){
var src = $("#cards-templ").html();
var templ = Handlebars.compile(src);
var html = templ(msg.data);
$('.cards').html(html);
},
error: function(msg){
if (debug) console.log('Error: ', msg);
}
});
<div class="cards">
<script id="cards-templ" type="text/x-handlebars-template">
{{#each cards}}
<div class="item">
<img src="{{image}}" alt="{{name}}">
</div>
{{/each}}
</script>
</div>
Answer the question
In order to leave comments, you need to log in
I think I found a solution:
Redefine the jQuery .html() function:
(function ($) {
// create a reference to the old `.html()` function
var htmlOriginal = $.fn.html;
// redefine the `.html()` function to accept a callback
$.fn.html = function(html,callback){
// run the old `.html()` function with the first parameter
var ret = htmlOriginal.apply(this, arguments);
// run the callback (if it is defined)
if(typeof callback == "function"){
callback();
}
// make sure chaining is not broken
return ret;
}
})(jQuery);
$('.cards').html(html, function(){
console.log("Данные успешно загружены и отображены");
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question