mirror of
https://github.com/S2-/ejs-render-remote
synced 2025-08-03 21:10:04 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
8c9f23ce3f | |||
94d5afe1a0 | |||
f5a44438d1 | |||
06b2289724 | |||
9e36c0e82b | |||
6047c6b084 | |||
4dd2bfb8a9 | |||
b6e19487b7 |
@@ -1,4 +1,4 @@
|
|||||||
//this uses jQuery for now because ie11 support is needed (promises and fetch)
|
//this uses jQuery for now because ie11 support is needed (promises, fetch, Object.assign)
|
||||||
|
|
||||||
(function($) {
|
(function($) {
|
||||||
var uuidv4 = function() {
|
var uuidv4 = function() {
|
||||||
@@ -9,17 +9,22 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
ejs.rr = function(templateUrl, data) {
|
var overwriteWithCacheOptions = function(options, cacheName) {
|
||||||
|
var cacheOptions = {
|
||||||
|
cache: true,
|
||||||
|
filename: cacheName
|
||||||
|
};
|
||||||
|
|
||||||
|
var templateOptions = options || {};
|
||||||
|
|
||||||
|
return $.extend(templateOptions, cacheOptions);
|
||||||
|
};
|
||||||
|
|
||||||
|
ejs.rr = function(templateUrl, data, options) {
|
||||||
var templateFn = ejs.cache.get(templateUrl);
|
var templateFn = ejs.cache.get(templateUrl);
|
||||||
|
|
||||||
//if the template is already cached, return it and we are done
|
//if the template is already cached, return it and we are done
|
||||||
if (templateFn) {
|
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);
|
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
|
} 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
|
||||||
@@ -33,35 +38,35 @@
|
|||||||
|
|
||||||
var r = uuidv4();
|
var r = uuidv4();
|
||||||
getTemplateFn.then(function(template) {
|
getTemplateFn.then(function(template) {
|
||||||
|
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
||||||
|
|
||||||
$('#' + r).replaceWith(ejs.render(
|
$('#' + r).replaceWith(ejs.render(
|
||||||
template,
|
template,
|
||||||
data,
|
data,
|
||||||
{
|
templateOptions
|
||||||
cache: true,
|
|
||||||
filename: templateUrl
|
|
||||||
}
|
|
||||||
));
|
));
|
||||||
|
|
||||||
|
//clean up the getFnFor
|
||||||
|
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
|
||||||
|
ejs.cache.remove('getFnFor' + templateUrl);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
|
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ejs.preloadTemplate = function(templateUrl) {
|
ejs.preloadTemplate = function(templateUrl, options) {
|
||||||
var d = $.Deferred();
|
var d = $.Deferred();
|
||||||
|
|
||||||
//if the template is already cached, just return.
|
//if the template is already cached, just return.
|
||||||
if (ejs.cache.get(templateUrl)) {
|
if (ejs.cache.get(templateUrl)) {
|
||||||
d.resolve();
|
d.resolve(templateUrl);
|
||||||
} else {
|
} else {
|
||||||
$.get(templateUrl)
|
$.get(templateUrl)
|
||||||
.then(function(template) {
|
.then(function(template) {
|
||||||
var templateFn = ejs.compile(template,
|
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
||||||
{
|
var templateFn = ejs.compile(template, templateOptions);
|
||||||
cache: true,
|
|
||||||
filename: templateUrl
|
|
||||||
});
|
|
||||||
|
|
||||||
ejs.cache.set(templateUrl, templateFn);
|
ejs.cache.set(templateUrl, templateFn);
|
||||||
|
|
||||||
d.resolve(templateUrl);
|
d.resolve(templateUrl);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ejs-render-remote",
|
"name": "ejs-render-remote",
|
||||||
"version": "1.0.3",
|
"version": "1.0.7",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"homepage": "https://github.com/S2-/ejs-render-remote",
|
"homepage": "https://github.com/S2-/ejs-render-remote",
|
||||||
|
Reference in New Issue
Block a user