1
0
mirror of https://github.com/S2-/ejs-render-remote synced 2025-08-04 13:20:06 +02:00

1 Commits

Author SHA1 Message Date
s2
37a2512f4b v1.0.3 2019-04-14 22:05:08 +02:00
2 changed files with 20 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
//this uses jQuery for now because ie11 support is needed (promises, fetch, Object.assign)
//this uses jQuery for now because ie11 support is needed (promises and fetch)
(function($) {
var uuidv4 = function() {
@@ -9,22 +9,17 @@
});
};
var overwriteWithCacheOptions = function(options, cacheName) {
var cacheOptions = {
cache: true,
filename: cacheName
};
var templateOptions = options || {};
return $.extend(templateOptions, cacheOptions);
};
ejs.rr = function(templateUrl, data, options) {
ejs.rr = 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
@@ -38,25 +33,21 @@
var r = uuidv4();
getTemplateFn.then(function(template) {
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
$('#' + r).replaceWith(ejs.render(
template,
data,
templateOptions
));
//clean up the getFnFor
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
ejs.cache.remove('getFnFor' + templateUrl);
{
cache: true,
filename: templateUrl
}
));
});
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
}
};
ejs.preloadTemplate = function(templateUrl, options) {
ejs.preloadTemplate = function(templateUrl) {
var d = $.Deferred();
//if the template is already cached, just return.
@@ -65,8 +56,12 @@
} else {
$.get(templateUrl)
.then(function(template) {
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
var templateFn = ejs.compile(template, templateOptions);
var templateFn = ejs.compile(template,
{
cache: true,
filename: templateUrl
});
ejs.cache.set(templateUrl, templateFn);
d.resolve(templateUrl);

View File

@@ -1,6 +1,6 @@
{
"name": "ejs-render-remote",
"version": "1.0.6",
"version": "1.0.3",
"description": "",
"main": "index.js",
"homepage": "https://github.com/S2-/ejs-render-remote",