ejs-render-remote 1.0.13 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/ejs-render-remote.js +51 -37
- package/examples/index.html +3 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ See `examples` folder.
|
|
|
27
27
|
|
|
28
28
|
### ejs.rr(templateUrl, data)
|
|
29
29
|
|
|
30
|
-
`ejs.rr` (render remote) renders the remote template. It
|
|
30
|
+
`ejs.rr` (render remote) renders the remote template. It fetches the template and then `ejs.render`s it.
|
|
31
31
|
The resulting ejs template function is cached, so the second time this function is invoked for that same template, `ejs.rr` returns the rendered template synchronously.
|
|
32
32
|
|
|
33
33
|
### ejs.preloadTemplate(templateUrl)
|
package/ejs-render-remote.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
(function($) {
|
|
1
|
+
(function() {
|
|
4
2
|
var uuidv4 = function() {
|
|
5
3
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
6
4
|
var r = Math.random() * 16 | 0;
|
|
@@ -17,7 +15,7 @@
|
|
|
17
15
|
|
|
18
16
|
var templateOptions = options || {};
|
|
19
17
|
|
|
20
|
-
return
|
|
18
|
+
return Object.assign(templateOptions, cacheOptions);
|
|
21
19
|
};
|
|
22
20
|
|
|
23
21
|
ejs.rr = function(templateUrl, data, options) {
|
|
@@ -30,30 +28,41 @@
|
|
|
30
28
|
} 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
|
|
31
29
|
|
|
32
30
|
//is there a getFn for this template?
|
|
33
|
-
var
|
|
34
|
-
if (!
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
var getTemplatePromise = ejs.cache.get('getFnFor' + templateUrl);
|
|
32
|
+
if (!getTemplatePromise) {
|
|
33
|
+
getTemplatePromise = fetch(templateUrl).then(response => {
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
throw new Error('Failed to fetch template: ' + response.statusText);
|
|
36
|
+
}
|
|
37
|
+
return response.text();
|
|
38
|
+
});
|
|
39
|
+
ejs.cache.set('getFnFor' + templateUrl, getTemplatePromise);
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
var r = uuidv4();
|
|
40
|
-
|
|
43
|
+
getTemplatePromise.then(function(template) {
|
|
41
44
|
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
|
42
45
|
|
|
43
46
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
var element = document.getElementById(r);
|
|
48
|
+
if (element) {
|
|
49
|
+
element.outerHTML = ejs.render(
|
|
50
|
+
template,
|
|
51
|
+
data,
|
|
52
|
+
templateOptions
|
|
53
|
+
);
|
|
54
|
+
}
|
|
49
55
|
} catch(ex) {
|
|
50
|
-
|
|
56
|
+
console.error(ex);
|
|
57
|
+
throw ex;
|
|
51
58
|
}
|
|
52
59
|
|
|
53
60
|
//clean up the getFnFor
|
|
54
61
|
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
|
|
55
62
|
ejs.cache.remove('getFnFor' + templateUrl);
|
|
56
63
|
}
|
|
64
|
+
}).catch(function(ex) {
|
|
65
|
+
console.error('Error loading template:', ex);
|
|
57
66
|
});
|
|
58
67
|
|
|
59
68
|
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
|
|
@@ -61,26 +70,31 @@
|
|
|
61
70
|
};
|
|
62
71
|
|
|
63
72
|
ejs.preloadTemplate = function(templateUrl, options) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
73
|
+
return new Promise(function(resolve, reject) {
|
|
74
|
+
//if the template is already cached, just return.
|
|
75
|
+
if (ejs.cache.get(templateUrl)) {
|
|
76
|
+
resolve(templateUrl);
|
|
77
|
+
} else {
|
|
78
|
+
fetch(templateUrl)
|
|
79
|
+
.then(function(response) {
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
throw new Error('Failed to fetch template: ' + response.statusText);
|
|
82
|
+
}
|
|
83
|
+
return response.text();
|
|
84
|
+
})
|
|
85
|
+
.then(function(template) {
|
|
86
|
+
try {
|
|
87
|
+
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
|
|
88
|
+
var templateFn = ejs.compile(template, templateOptions);
|
|
89
|
+
ejs.cache.set(templateUrl, templateFn);
|
|
90
|
+
resolve(templateUrl);
|
|
91
|
+
} catch(ex) {
|
|
92
|
+
console.error(ex);
|
|
93
|
+
reject(ex);
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
.catch(reject);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
85
99
|
};
|
|
86
|
-
})(
|
|
100
|
+
})();
|
package/examples/index.html
CHANGED
|
@@ -11,21 +11,20 @@
|
|
|
11
11
|
<p class="hello"></p>
|
|
12
12
|
<div class="sometext"></div>
|
|
13
13
|
|
|
14
|
-
<script src="jquery.min.js"></script>
|
|
15
14
|
<script src="ejs.min.js"></script>
|
|
16
15
|
<script src="../ejs-render-remote.js"></script>
|
|
17
16
|
|
|
18
17
|
|
|
19
18
|
<script>
|
|
20
19
|
//render the template
|
|
21
|
-
|
|
20
|
+
document.querySelector('.hello').innerHTML = ejs.rr('templates/hello.ejs', {name: 'Simon'});
|
|
22
21
|
|
|
23
22
|
//a more elaborate example: preload the template to be sure the dom is
|
|
24
23
|
//ready when manipulated
|
|
25
24
|
ejs.preloadTemplate('templates/somestuff.ejs')
|
|
26
25
|
.then(function(t) {
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
document.querySelector('.sometext').innerHTML = ejs.rr(t); //this is sync now
|
|
27
|
+
document.querySelector('.stuff').innerHTML = 'hi!';
|
|
29
28
|
});
|
|
30
29
|
</script>
|
|
31
30
|
</body>
|