From 7c2a6f215734d29f3be8c0a0c85307e896f48155 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 9 Sep 2018 03:59:46 -0400 Subject: js: add cache singleton, support non-array cache templates --- js/luigi-template.js | 8 ++++++-- js/test/cache.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'js') diff --git a/js/luigi-template.js b/js/luigi-template.js index d48d668..473938f 100644 --- a/js/luigi-template.js +++ b/js/luigi-template.js @@ -300,8 +300,8 @@ LuigiTemplate = (function() { var s = null; if (key in this.templates) { - // get template from constructor templates - s = this.templates[key].join(''); + s = this.templates[key]; + s = (s.constructor === Array) ? s.join('') : s; } else if (this.try_dom) { // get source from inline script tag s = get_inline_template(key); @@ -317,6 +317,10 @@ LuigiTemplate = (function() { return this.cache[key].run(args); }; + T.cache = function(args) { + return new T.Cache(args); + } + // declare domcache constructor T.DOMCache = function() { this.cache = {}; diff --git a/js/test/cache.js b/js/test/cache.js index 39dd46d..0b68b3d 100644 --- a/js/test/cache.js +++ b/js/test/cache.js @@ -3,6 +3,18 @@ var assert = chai.assert; it('cache', function() { + var cache = new LuigiTemplate.Cache({ + foo: 'foo%{bar}', + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foofoo'); + }); + + it('cache with array', function() { var cache = new LuigiTemplate.Cache({ foo: ['foo%{bar}'], }); @@ -14,6 +26,19 @@ assert.equal(r, 'foofoo'); }); + it('cache singleton', function() { + var cache = LuigiTemplate.cache({ + foo: 'foo%{bar}', + }); + + var r = cache.run('foo', { + bar: 'foo', + }); + + assert.equal(r, 'foofoo'); + }); + + it('cache with custom filters', function() { var cache = new LuigiTemplate.Cache({ foo: ['foo%{bar | barify}'], -- cgit v1.2.3