From 5f3f50f81a422c5a8cf20b277c89d185d408daeb Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Tue, 24 Feb 2015 10:35:31 -0800 Subject: [PATCH 1/2] add serverToClientJson helper - this should be set when you pass any data from the server to the client --- package.json | 4 ++-- shared/helpers.js | 1 + shared/helpers/serverToClientJson.js | 6 ++++++ test/shared/helpers.test.js | 1 + test/shared/helpers/serverToClientJson.test.js | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 shared/helpers/serverToClientJson.js create mode 100644 test/shared/helpers/serverToClientJson.test.js diff --git a/package.json b/package.json index b2fa710..2e499de 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "url": "http://github.com/rendrjs/rendr-handlebars.git" }, "dependencies": { - "underscore": "~1.7.0", - "handlebars": "1.3.0" + "handlebars": "1.3.0", + "underscore": "~1.7.0" }, "peerDependencies": { "rendr": ">=0.5.1" diff --git a/shared/helpers.js b/shared/helpers.js index 46c3942..f824d89 100644 --- a/shared/helpers.js +++ b/shared/helpers.js @@ -4,6 +4,7 @@ module.exports = function(Handlebars, getTemplate) { partial: require('./helpers/partial')(Handlebars, getTemplate), json: require('./helpers/json')(Handlebars), each: require('./helpers/each')(Handlebars), + serverToClientJson: require('./helpers/serverToClientJson')(Handlebars), forEach: require('./helpers/forEach') }; }; diff --git a/shared/helpers/serverToClientJson.js b/shared/helpers/serverToClientJson.js new file mode 100644 index 0000000..bdcc328 --- /dev/null +++ b/shared/helpers/serverToClientJson.js @@ -0,0 +1,6 @@ +module.exports = function (Handlebars) { + return function (obj) { + var data = escape(JSON.stringify(obj)); + return new Handlebars.SafeString('JSON.parse(unescape("' + data + '"))'); + }; +}; diff --git a/test/shared/helpers.test.js b/test/shared/helpers.test.js index b729a03..abbdbe2 100644 --- a/test/shared/helpers.test.js +++ b/test/shared/helpers.test.js @@ -21,5 +21,6 @@ describe('helpers', function () { expect(subject.forEach).to.be.a('function'); expect(subject.partial).to.be.a('function'); expect(subject.view).to.be.a('function'); + expect(subject.serverToClientJson).to.be.a('function'); }); }); diff --git a/test/shared/helpers/serverToClientJson.test.js b/test/shared/helpers/serverToClientJson.test.js new file mode 100644 index 0000000..f4678d6 --- /dev/null +++ b/test/shared/helpers/serverToClientJson.test.js @@ -0,0 +1,14 @@ +var Handlebars = require('handlebars').create(), + sinon = require('sinon'), + chai = require('chai'), + expect = chai.expect, + subject = require('../../../shared/helpers/serverToClientJson')(Handlebars); + +describe('serverToClientJson', function () { + var data = { key: 'права' } + + it('should add JSON.parse and unescape to the string', function () { + var result = subject(data); + expect(eval(result.string)).to.deep.equal(data); + }); +}); From 7631e80ba44854f431bf4c7e08d9716cf4901c58 Mon Sep 17 00:00:00 2001 From: Josh Callender Date: Tue, 24 Feb 2015 16:44:58 -0800 Subject: [PATCH 2/2] verify that the string is what we'd want and the data is parsed the way we expect --- test/shared/helpers/serverToClientJson.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/shared/helpers/serverToClientJson.test.js b/test/shared/helpers/serverToClientJson.test.js index f4678d6..dff3809 100644 --- a/test/shared/helpers/serverToClientJson.test.js +++ b/test/shared/helpers/serverToClientJson.test.js @@ -8,7 +8,15 @@ describe('serverToClientJson', function () { var data = { key: 'права' } it('should add JSON.parse and unescape to the string', function () { + var result = subject(data), + expectedResult = 'JSON.parse(unescape("%7B%22key%22%3A%22%u043F%u0440%u0430%u0432%u0430%22%7D"))'; + + expect(expectedResult).to.equal(result.string); + }); + + it('should result in the same data after eval', function () { var result = subject(data); expect(eval(result.string)).to.deep.equal(data); }); + });