From d4fa8080321defd8fb25083e8704ac6c5a6e24ce Mon Sep 17 00:00:00 2001 From: Blueeye1015 Date: Thu, 19 Mar 2015 17:54:55 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=8C=E5=96=84template.remove=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/underscore-template.js | 14 ++++++++++++-- test/test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/underscore-template.js b/src/underscore-template.js index 0de8c85..4e21027 100644 --- a/src/underscore-template.js +++ b/src/underscore-template.js @@ -111,9 +111,19 @@ var template = function () { } //api - template.remove = function (/* id */) { + template.remove = function (id) { //todo: remove template from cache (both str and fn) //todo: remove dummy script element + if (!id) return false + + var templateId = _toTemplateId(id) + if (!_cacheTemplate[templateId]) { + console.warn('[Template] Template id "' + templateId + '" can not be found.') + return false + } + + delete _cacheTemplate[templateId] + delete _cacheCompiledTemplate[templateId] } template.add = add template.render = function (id, data) { @@ -129,7 +139,7 @@ var template = function () { //search in _cacheCompiledTemplate var fn = _cacheCompiledTemplate[templateId] var templateCode = _cacheTemplate[templateId] - if (_.isFunction(fn)) { + if (_.isFunction(fn)) { result = fn(data) } //search in _cacheTemplate diff --git a/test/test.js b/test/test.js index 60bec26..13e59d1 100644 --- a/test/test.js +++ b/test/test.js @@ -333,6 +333,38 @@ void function () { }) }) + describe('template.remove()', function () { + it('returns false if template id is invalid or an empty string', function () { + var arg + arg = null + expect(template.remove(arg)).to.be.false + arg = undefined + expect(template.remove(arg)).to.be.false + arg = '' + expect(template.remove(arg)).to.be.false + }) + it('returns false if template id is not found', function (){ + var arg + arg = 'not-existed' + expect(template.remove(arg)).to.be.false + }) + it('removes template from cache', function () { + expect(_cacheTemplate).to.deep.equal({}) + expect(_cacheCompiledTemplate).to.deep.equal({}) + prepareDummyScript() + + template.add(TEMPLATE_ID_1, templateCode1) + template.remove(TEMPLATE_ID_1) + expect(_cacheTemplate).to.deep.equal({}) + expect(_cacheCompiledTemplate).to.deep.equal({}) + + template.add(TEMPLATE_ID_2, templateCode2) + template.remove(TEMPLATE_ID_2) + expect(_cacheTemplate).to.deep.equal({}) + expect(_cacheCompiledTemplate).to.deep.equal({}) + }) + }) + describe('template.render()', function () { it('gets template from dom, renders, and saves to cache', function () { expect(_cacheTemplate).to.deep.equal({}) From a2118a00541d304f9da93ac980defef4b7b7ff60 Mon Sep 17 00:00:00 2001 From: Blueeye1015 Date: Fri, 20 Mar 2015 14:25:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=A9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/underscore-template.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/underscore-template.js b/src/underscore-template.js index 4e21027..01bc531 100644 --- a/src/underscore-template.js +++ b/src/underscore-template.js @@ -139,7 +139,7 @@ var template = function () { //search in _cacheCompiledTemplate var fn = _cacheCompiledTemplate[templateId] var templateCode = _cacheTemplate[templateId] - if (_.isFunction(fn)) { + if (_.isFunction(fn)) { result = fn(data) } //search in _cacheTemplate From 4b7be8ba01a8bcf43aac5bf1bee94b99563c2cbc Mon Sep 17 00:00:00 2001 From: Blueeye1015 Date: Fri, 20 Mar 2015 14:34:30 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 13e59d1..4f7d3ee 100644 --- a/test/test.js +++ b/test/test.js @@ -343,7 +343,7 @@ void function () { arg = '' expect(template.remove(arg)).to.be.false }) - it('returns false if template id is not found', function (){ + it('returns false if template id is not found', function () { var arg arg = 'not-existed' expect(template.remove(arg)).to.be.false @@ -353,12 +353,15 @@ void function () { expect(_cacheCompiledTemplate).to.deep.equal({}) prepareDummyScript() + //remove a template which is not compiled template.add(TEMPLATE_ID_1, templateCode1) template.remove(TEMPLATE_ID_1) expect(_cacheTemplate).to.deep.equal({}) expect(_cacheCompiledTemplate).to.deep.equal({}) + //remove a template which has been compiled template.add(TEMPLATE_ID_2, templateCode2) + template.render(TEMPLATE_ID_2) template.remove(TEMPLATE_ID_2) expect(_cacheTemplate).to.deep.equal({}) expect(_cacheCompiledTemplate).to.deep.equal({}) From b157deebe72453f9abdd3afb7c68ffcec1c76acf Mon Sep 17 00:00:00 2001 From: Blueeye1015 Date: Fri, 20 Mar 2015 17:35:05 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=AD=E7=9A=84bug=EF=BC=8C=E6=B8=85?= =?UTF-8?q?=E9=99=A4template.js=E4=B8=AD=E5=A4=9A=E4=BD=99=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/underscore-template.js | 1 - test/test.js | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/underscore-template.js b/src/underscore-template.js index 01bc531..8849662 100644 --- a/src/underscore-template.js +++ b/src/underscore-template.js @@ -112,7 +112,6 @@ var template = function () { //api template.remove = function (id) { - //todo: remove template from cache (both str and fn) //todo: remove dummy script element if (!id) return false diff --git a/test/test.js b/test/test.js index 4f7d3ee..0d503d8 100644 --- a/test/test.js +++ b/test/test.js @@ -351,7 +351,6 @@ void function () { it('removes template from cache', function () { expect(_cacheTemplate).to.deep.equal({}) expect(_cacheCompiledTemplate).to.deep.equal({}) - prepareDummyScript() //remove a template which is not compiled template.add(TEMPLATE_ID_1, templateCode1) @@ -361,7 +360,7 @@ void function () { //remove a template which has been compiled template.add(TEMPLATE_ID_2, templateCode2) - template.render(TEMPLATE_ID_2) + template.render(TEMPLATE_ID_2, templateData2) template.remove(TEMPLATE_ID_2) expect(_cacheTemplate).to.deep.equal({}) expect(_cacheCompiledTemplate).to.deep.equal({})