From af02b6052da0ab0317df3b37221747425147f7a0 Mon Sep 17 00:00:00 2001 From: YuQian Date: Wed, 21 Oct 2015 10:20:51 +0800 Subject: [PATCH] =?UTF-8?q?fix`.add()`=E6=97=A0=E6=B3=95=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E5=90=8C=E5=90=8D=E6=A8=A1=E6=9D=BF=E5=B9=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=9B=B8=E5=BA=94=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/underscore-template.js | 5 ++++- test/test.js | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/underscore-template.js b/src/underscore-template.js index 8e27801..2eb00d1 100644 --- a/src/underscore-template.js +++ b/src/underscore-template.js @@ -98,10 +98,13 @@ var template = function () { if (templateCode) { var templateId = _toTemplateId(id) /** DEBUG_INFO_START **/ - if (_cacheTemplate[templateId]) { + if (_cacheTemplate[templateId] || _cacheCompiledTemplate[templateId]) { console.warn('[Template] Template id "' + templateId + '" already existed.') } /** DEBUG_INFO_END **/ + if (_cacheCompiledTemplate[templateId]) { + _cacheCompiledTemplate[templateId] = null + } result = _cacheTemplate[templateId] = templateCode } else { //todo: support `_.template.add(id)` to add from dummy script element diff --git a/test/test.js b/test/test.js index 6680d71..aebebf1 100644 --- a/test/test.js +++ b/test/test.js @@ -21,6 +21,10 @@ void function () { '' ].join('\n') + //compiledTemplate + var fn1 = _.template(templateCode1) + var fn2 = _.template(templateCode2) + //template data var templateData1 = {text: HELLO} var templateData2 = [ @@ -328,7 +332,7 @@ void function () { expect(_cacheTemplate).to.deep.equal(data) expect(_cacheCompiledTemplate).to.deep.equal({}) }) - it('overwrites while adding existed template id', function () { + it('overwrites template while adding existed template id', function () { expect(_cacheTemplate).to.deep.equal({}) data = {} @@ -336,10 +340,23 @@ void function () { data[TEMPLATE_ID_1] = templateCode1 expect(_cacheTemplate).to.deep.equal(data) - template.add(TEMPLATE_ID_2, templateCode2) - data[TEMPLATE_ID_2] = templateCode2 + template.add(TEMPLATE_ID_1, templateCode2) + data[TEMPLATE_ID_1] = templateCode2 expect(_cacheTemplate).to.deep.equal(data) }) + it('overwrites compiledTemplate while adding existed template id', function () { + expect(_cacheCompiledTemplate).to.deep.equal({}) + + template.add(TEMPLATE_ID_1, templateCode1) + template.render(TEMPLATE_ID_1, templateData1) + expect(_cacheCompiledTemplate[TEMPLATE_ID_1].source).to.equal(fn1.source) + + template.add(TEMPLATE_ID_1, templateCode2) + expect(_cacheCompiledTemplate[TEMPLATE_ID_1]).to.equal(null) + template.render(TEMPLATE_ID_1, templateData2) + expect(_cacheCompiledTemplate[TEMPLATE_ID_1]).to.be.a('function') + expect(_cacheCompiledTemplate[TEMPLATE_ID_1].source).to.equal(fn2.source) + }) it('does nothing if missing template code as second param', function () { expect(_cacheTemplate).to.deep.equal({}) template.add('foo')