Switch to async rendering.

This commit is contained in:
Michael Teeuw 2017-09-28 16:43:38 +02:00
parent e01794a07f
commit 5efc43260e

View File

@ -81,23 +81,26 @@ var Module = Class.extend({
* return domobject - The dom to display. * return domobject - The dom to display.
*/ */
getDom: function () { getDom: function () {
var div = document.createElement("div");
var template = this.getTemplate(); var template = this.getTemplate();
var templateData = this.getTemplateData(); var templateData = this.getTemplateData();
// Check to see if we need to render a template string or a file. // Check to see if we need to render a template string or a file.
if (/^.*(\.html)$/.test(template)) { if (/^.*(\.html)$/.test(template)) {
// the template is a filename // the template is a filename
var filename = this.file(template); this.nunjucksEnvironment().render(template, templateData, function (err, res) {
var content = this.nunjucksEnvironment().render(filename, templateData); // The inner content of the div will be set after the template is received.
// This isn't the most optimal way, but since it's near instant
// it probably won't be an issue.
// If it gives problems, we can always add a way to pre fetch the templates.
// Let's not over optimise this ... KISS! :)
div.innerHTML = res;
});
} else { } else {
// the template is a template string. // the template is a template string.
var content = this.nunjucksEnvironment().renderString(template, templateData); div.innerHTML = this.nunjucksEnvironment().renderString(template, templateData);
} }
var div = document.createElement("div");
div.innerHTML = content;
return div; return div;
}, },
@ -163,7 +166,7 @@ var Module = Class.extend({
var self = this; var self = this;
this._nunjucksEnvironment = new nunjucks.Environment(new nunjucks.WebLoader()); this._nunjucksEnvironment = new nunjucks.Environment(new nunjucks.WebLoader(this.file(""), {async: true}));
this._nunjucksEnvironment.addFilter("translate", function(str) { this._nunjucksEnvironment.addFilter("translate", function(str) {
return self.translate(str) return self.translate(str)
}); });