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

17 Commits

Author SHA1 Message Date
s2
21d9319f97 1.0.12 2020-02-08 22:38:28 +01:00
s2
371d16bc2e s/then/done/ 2020-02-08 22:37:54 +01:00
s2
d53c239f90 no-console 2020-02-08 18:24:09 +01:00
s2
6635965706 1.0.11 2020-02-08 18:22:48 +01:00
s2
dfcf1a4b6f use $.readyException 2020-02-08 18:22:16 +01:00
s2
4b3df71c6b 1.0.10 2020-02-08 17:57:16 +01:00
s2
af63b77c9b add more logs 2020-02-08 17:57:05 +01:00
s2
9aba0176f4 add an example 2020-02-08 17:05:22 +01:00
s2
75401b90dd update ejs 2020-02-08 16:58:53 +01:00
s2
47459bd322 1.0.9 2020-02-07 20:50:08 +01:00
s2
6ed2b48d36 log async ejs compile error to console 2020-02-07 20:50:01 +01:00
s2
23d46035e8 1.0.8 2020-02-07 17:29:59 +01:00
s2
1c720ce9f3 when compile throws an exception, log it to the console 2020-02-07 17:26:54 +01:00
s2
8c9f23ce3f v1.0.7 2019-05-18 15:30:59 +02:00
s2
94d5afe1a0 resolve with templateUrl even when we have it already in cache 2019-05-18 15:30:59 +02:00
s2
f5a44438d1 v1.0.6 2019-05-18 15:30:59 +02:00
s2
06b2289724 use $.extend 2019-05-18 15:30:59 +02:00
7 changed files with 48 additions and 24 deletions

View File

@@ -28,6 +28,7 @@
"dot-notation": 0, "dot-notation": 0,
"no-multi-str": 2, "no-multi-str": 2,
"key-spacing": [2, {"afterColon": true}], "key-spacing": [2, {"afterColon": true}],
"func-call-spacing": [2, "never"] "func-call-spacing": [2, "never"],
"no-console": "warn"
} }
} }

View File

@@ -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() {
@@ -16,11 +16,8 @@
}; };
var templateOptions = options || {}; var templateOptions = options || {};
for (var attrname in cacheOptions) {
templateOptions[attrname] = cacheOptions[attrname];
}
return templateOptions; return $.extend(templateOptions, cacheOptions);
}; };
ejs.rr = function(templateUrl, data, options) { ejs.rr = function(templateUrl, data, options) {
@@ -28,7 +25,11 @@
//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) {
try {
return templateFn(data); return templateFn(data);
} catch(ex) {
$.readyException(ex);
}
} 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
@@ -40,14 +41,18 @@
} }
var r = uuidv4(); var r = uuidv4();
getTemplateFn.then(function(template) { getTemplateFn.done(function(template) {
var templateOptions = overwriteWithCacheOptions(options, templateUrl); var templateOptions = overwriteWithCacheOptions(options, templateUrl);
try {
$('#' + r).replaceWith(ejs.render( $('#' + r).replaceWith(ejs.render(
template, template,
data, data,
templateOptions templateOptions
)); ));
} catch(ex) {
$.readyException(ex);
}
//clean up the getFnFor //clean up the getFnFor
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) { if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
@@ -64,15 +69,19 @@
//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) { .done(function(template) {
try {
var templateOptions = overwriteWithCacheOptions(options, templateUrl); var templateOptions = overwriteWithCacheOptions(options, templateUrl);
var templateFn = ejs.compile(template, templateOptions); var templateFn = ejs.compile(template, templateOptions);
ejs.cache.set(templateUrl, templateFn); ejs.cache.set(templateUrl, templateFn);
d.resolve(templateUrl); d.resolve(templateUrl);
} catch(ex) {
$.readyException(ex);
d.reject(ex);
}
}); });
} }

2
examples/ejs.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -9,6 +9,7 @@
<body> <body>
<h1>ejs-render-remote example</h1> <h1>ejs-render-remote example</h1>
<p class="hello"></p> <p class="hello"></p>
<div class="sometext"></div>
<script src="jquery.min.js"></script> <script src="jquery.min.js"></script>
<script src="ejs.min.js"></script> <script src="ejs.min.js"></script>
@@ -18,6 +19,14 @@
<script> <script>
//render the template //render the template
$('.hello').html(ejs.rr('templates/hello.ejs', {name: 'Simon'})); $('.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> </script>
</body> </body>
</html> </html>

View File

@@ -0,0 +1 @@
<p class="stuff"></p>

View File

@@ -1,6 +1,6 @@
{ {
"name": "ejs-render-remote", "name": "ejs-render-remote",
"version": "1.0.5", "version": "1.0.12",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"homepage": "https://github.com/S2-/ejs-render-remote", "homepage": "https://github.com/S2-/ejs-render-remote",
@@ -9,5 +9,9 @@
}, },
"author": "s2 <s2@31337.it>", "author": "s2 <s2@31337.it>",
"license": "ISC", "license": "ISC",
"keywords": ["ejs", "templating", "client-side"] "keywords": [
"ejs",
"templating",
"client-side"
]
} }