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 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 makes an ajax call to fetch the template and then `ejs.render`s 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)
@@ -1,6 +1,4 @@
1
- //this uses jQuery for now because ie11 support is needed (promises, fetch, Object.assign)
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 $.extend(templateOptions, cacheOptions);
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 getTemplateFn = ejs.cache.get('getFnFor' + templateUrl);
34
- if (!getTemplateFn) {
35
- getTemplateFn = $.get(templateUrl);
36
- ejs.cache.set('getFnFor' + templateUrl, getTemplateFn);
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
- getTemplateFn.done(function(template) {
43
+ getTemplatePromise.then(function(template) {
41
44
  var templateOptions = overwriteWithCacheOptions(options, templateUrl);
42
45
 
43
46
  try {
44
- $('#' + r).replaceWith(ejs.render(
45
- template,
46
- data,
47
- templateOptions
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
- $.readyException(ex);
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
- var d = $.Deferred();
65
-
66
- //if the template is already cached, just return.
67
- if (ejs.cache.get(templateUrl)) {
68
- d.resolve(templateUrl);
69
- } else {
70
- $.get(templateUrl)
71
- .done(function(template) {
72
- try {
73
- var templateOptions = overwriteWithCacheOptions(options, templateUrl);
74
- var templateFn = ejs.compile(template, templateOptions);
75
- ejs.cache.set(templateUrl, templateFn);
76
- d.resolve(templateUrl);
77
- } catch(ex) {
78
- $.readyException(ex);
79
- d.reject(ex);
80
- }
81
- });
82
- }
83
-
84
- return d;
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
- })(jQuery);
100
+ })();
@@ -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
- $('.hello').html(ejs.rr('templates/hello.ejs', {name: 'Simon'}));
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
- $('.sometext').html(ejs.rr(t)); //this is sync now
28
- $('.stuff').html('hi!');
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>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ejs-render-remote",
3
- "version": "1.0.13",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "homepage": "https://github.com/S2-/ejs-render-remote",