Skip to content

Commit f1ee88e

Browse files
committed
Merge pull request #48 from rendrjs/server-2-client-json
add serverToClientJson helper
2 parents d7b4f62 + 7631e80 commit f1ee88e

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"url": "http://github.com/rendrjs/rendr-handlebars.git"
1212
},
1313
"dependencies": {
14-
"underscore": "~1.7.0",
15-
"handlebars": "1.3.0"
14+
"handlebars": "1.3.0",
15+
"underscore": "~1.7.0"
1616
},
1717
"peerDependencies": {
1818
"rendr": ">=0.5.1"

shared/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = function(Handlebars, getTemplate) {
44
partial: require('./helpers/partial')(Handlebars, getTemplate),
55
json: require('./helpers/json')(Handlebars),
66
each: require('./helpers/each')(Handlebars),
7+
serverToClientJson: require('./helpers/serverToClientJson')(Handlebars),
78
forEach: require('./helpers/forEach')
89
};
910
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (Handlebars) {
2+
return function (obj) {
3+
var data = escape(JSON.stringify(obj));
4+
return new Handlebars.SafeString('JSON.parse(unescape("' + data + '"))');
5+
};
6+
};

test/shared/helpers.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ describe('helpers', function () {
2121
expect(subject.forEach).to.be.a('function');
2222
expect(subject.partial).to.be.a('function');
2323
expect(subject.view).to.be.a('function');
24+
expect(subject.serverToClientJson).to.be.a('function');
2425
});
2526
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var Handlebars = require('handlebars').create(),
2+
sinon = require('sinon'),
3+
chai = require('chai'),
4+
expect = chai.expect,
5+
subject = require('../../../shared/helpers/serverToClientJson')(Handlebars);
6+
7+
describe('serverToClientJson', function () {
8+
var data = { key: 'права' }
9+
10+
it('should add JSON.parse and unescape to the string', function () {
11+
var result = subject(data),
12+
expectedResult = 'JSON.parse(unescape("%7B%22key%22%3A%22%u043F%u0440%u0430%u0432%u0430%22%7D"))';
13+
14+
expect(expectedResult).to.equal(result.string);
15+
});
16+
17+
it('should result in the same data after eval', function () {
18+
var result = subject(data);
19+
expect(eval(result.string)).to.deep.equal(data);
20+
});
21+
22+
});

0 commit comments

Comments
 (0)