mirror of
https://github.com/S2-/ejs-render-remote
synced 2025-08-03 21:10:04 +02:00
Compare commits
1 Commits
v1.0.12
...
37a2512f4b
Author | SHA1 | Date | |
---|---|---|---|
37a2512f4b |
@@ -28,7 +28,6 @@
|
||||
"dot-notation": 0,
|
||||
"no-multi-str": 2,
|
||||
"key-spacing": [2, {"afterColon": true}],
|
||||
"func-call-spacing": [2, "never"],
|
||||
"no-console": "warn"
|
||||
"func-call-spacing": [2, "never"]
|
||||
}
|
||||
}
|
||||
|
@@ -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,28 +9,19 @@
|
||||
});
|
||||
};
|
||||
|
||||
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) {
|
||||
try {
|
||||
return templateFn(data);
|
||||
} catch(ex) {
|
||||
$.readyException(ex);
|
||||
//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?
|
||||
@@ -41,47 +32,39 @@
|
||||
}
|
||||
|
||||
var r = uuidv4();
|
||||
getTemplateFn.done(function(template) {
|
||||
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
||||
|
||||
try {
|
||||
$('#' + r).replaceWith(ejs.render(
|
||||
template,
|
||||
data,
|
||||
templateOptions
|
||||
));
|
||||
} catch(ex) {
|
||||
$.readyException(ex);
|
||||
}
|
||||
|
||||
//clean up the getFnFor
|
||||
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
|
||||
ejs.cache.remove('getFnFor' + templateUrl);
|
||||
}
|
||||
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>';
|
||||
}
|
||||
};
|
||||
|
||||
ejs.preloadTemplate = function(templateUrl, options) {
|
||||
ejs.preloadTemplate = function(templateUrl) {
|
||||
var d = $.Deferred();
|
||||
|
||||
//if the template is already cached, just return.
|
||||
if (ejs.cache.get(templateUrl)) {
|
||||
d.resolve(templateUrl);
|
||||
d.resolve();
|
||||
} else {
|
||||
$.get(templateUrl)
|
||||
.done(function(template) {
|
||||
try {
|
||||
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
||||
var templateFn = ejs.compile(template, templateOptions);
|
||||
ejs.cache.set(templateUrl, templateFn);
|
||||
d.resolve(templateUrl);
|
||||
} catch(ex) {
|
||||
$.readyException(ex);
|
||||
d.reject(ex);
|
||||
}
|
||||
.then(function(template) {
|
||||
var templateFn = ejs.compile(template,
|
||||
{
|
||||
cache: true,
|
||||
filename: templateUrl
|
||||
});
|
||||
|
||||
ejs.cache.set(templateUrl, templateFn);
|
||||
|
||||
d.resolve(templateUrl);
|
||||
});
|
||||
}
|
||||
|
||||
|
2
examples/ejs.min.js
vendored
2
examples/ejs.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -9,7 +9,6 @@
|
||||
<body>
|
||||
<h1>ejs-render-remote example</h1>
|
||||
<p class="hello"></p>
|
||||
<div class="sometext"></div>
|
||||
|
||||
<script src="jquery.min.js"></script>
|
||||
<script src="ejs.min.js"></script>
|
||||
@@ -19,14 +18,6 @@
|
||||
<script>
|
||||
//render the template
|
||||
$('.hello').html(ejs.rr('templates/hello.ejs', {name: 'Simon'}));
|
||||
|
||||
//a more elaborate example: preload the template to be sure the dom is
|
||||
//ready when manipulated
|
||||
ejs.preloadTemplate('templates/somestuff.ejs')
|
||||
.then(function(t) {
|
||||
$('.sometext').html(ejs.rr(t)); //this is sync now
|
||||
$('.stuff').html('hi!');
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@@ -1 +1 @@
|
||||
Hello <%= name %>!
|
||||
Hello <%= name%>!
|
||||
|
@@ -1 +0,0 @@
|
||||
<p class="stuff"></p>
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ejs-render-remote",
|
||||
"version": "1.0.12",
|
||||
"version": "1.0.3",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"homepage": "https://github.com/S2-/ejs-render-remote",
|
||||
@@ -9,9 +9,5 @@
|
||||
},
|
||||
"author": "s2 <s2@31337.it>",
|
||||
"license": "ISC",
|
||||
"keywords": [
|
||||
"ejs",
|
||||
"templating",
|
||||
"client-side"
|
||||
]
|
||||
"keywords": ["ejs", "templating", "client-side"]
|
||||
}
|
||||
|
Reference in New Issue
Block a user