diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5e9af4a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "eslint.enable": false +} \ No newline at end of file diff --git a/index.html b/index.html index d9f7c5a..cee970e 100644 --- a/index.html +++ b/index.html @@ -25,8 +25,8 @@ - + diff --git a/js/ejs-utils.js b/js/ejs-utils.js index 9701652..390e7d5 100644 --- a/js/ejs-utils.js +++ b/js/ejs-utils.js @@ -1,3 +1,41 @@ -ejs.rtfe = function(templateElement, data) { - return ejs.render($('#template-' + templateElement).html(), data); +ejs.rtfe = function(templateUrl, data) { + var templateFilename = templateUrl + '.ejs'; + var templateFn = ejs.cache.get(templateFilename); + + //if the template is already cached, return it and we are done + 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' + templateFilename)) { + ejs.cache.remove('getFnFor' + templateFilename); + } + + 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? + var getTemplateFn = ejs.cache.get('getFnFor' + templateFilename); + if (!getTemplateFn) { + getTemplateFn = $.get('/templates/' + templateFilename); + ejs.cache.set('getFnFor' + templateFilename, getTemplateFn); + } + + var r = Utils.uuidv4(); + getTemplateFn.then(function(template) { + $('#' + r).replaceWith( + ejs.render( + template, + data, + { + cache: true, + filename: templateFilename + } + ) + ); + }); + + return ``; + } + }; diff --git a/js/index.js b/js/index.js index 5bbae24..bffbfde 100644 --- a/js/index.js +++ b/js/index.js @@ -4,15 +4,8 @@ n3wz.apiPrefix = '/api/'; } - $.when( - $.get('templates.ejs'), - $.getJSON(n3wz.apiPrefix + 'newsgroup/it.comp.console/threads') - ) - .then(function(templates, threads) { - $('body').append(templates[0]); - - $('.js-articles').html(ejs.rtfe('articles', { - threads: threads[0].threads - })); + $.getJSON(n3wz.apiPrefix + 'newsgroup/it.comp.console/threads') + .then(function(threads) { + $('.js-articles').html(ejs.rtfe('articles', threads)); }); })(jQuery); diff --git a/templates/article.ejs b/templates/article.ejs new file mode 100644 index 0000000..675c690 --- /dev/null +++ b/templates/article.ejs @@ -0,0 +1,8 @@ +
+
+

<%= Utils.ellipsis(subject, 80) %>

+
<%= Utils.ellipsis(from ? from : from_email, 500) %>
+

<%= Utils.ellipsis(body, 500) %>

+ leggi tutto... +
+
diff --git a/templates/articles.ejs b/templates/articles.ejs new file mode 100644 index 0000000..00c413e --- /dev/null +++ b/templates/articles.ejs @@ -0,0 +1,10 @@ +<% +for (var i=0; i < threads.length - 1; i += 2) { +%> + +
+
<%- ejs.rtfe('article', threads[i]) %>
+
<%- ejs.rtfe('article', threads[i + 1]) %>
+
+ +<% } %>