Files
vanillajs-seed/js/thirdparty/ejs-utils.js
2019-04-12 20:01:07 +02:00

53 lines
1.5 KiB
JavaScript

//if this does not work, call Simon. https://github.com/S2-
(function($) {
var uuidv4 = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0;
var v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
ejs.rtfe = function(templateUrl, data) {
var templateFn = ejs.cache.get(templateUrl);
//if the template is already cached, return it and we are done
if (templateFn) {
//but first check if there is still a getter function for this template in the cache
//if yes, remove it so we clean up a bit
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
ejs.cache.remove('getFnFor' + templateUrl);
}
return templateFn(data);
} else { //if the template is not cached, we need to get it and render it later once we have it. remember: this happens only if the template is not already cached
//is there a getFn for this template?
var getTemplateFn = ejs.cache.get('getFnFor' + templateUrl);
if (!getTemplateFn) {
getTemplateFn = $.get(templateUrl);
ejs.cache.set('getFnFor' + templateUrl, getTemplateFn);
}
var r = uuidv4();
getTemplateFn.then(function(template) {
$('#' + r).replaceWith(
ejs.render(
template,
data,
{
cache: true,
filename: templateUrl
}
)
);
});
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
}
};
})(jQuery);