dust-usecontent-helper
Advanced tools
Comparing version 4.0.0-0 to 4.0.0-1
34
index.js
@@ -27,3 +27,4 @@ "use strict"; | ||
} else { | ||
ctx = ctx.push({intl: { messages: content }}); | ||
ctx = ctx.push({ intl: { messages: content } }); | ||
hackGibson(ctx, content); | ||
bodies.block(chunk, ctx).end(); | ||
@@ -36,2 +37,33 @@ } | ||
function hackGibson(ctx, content) { | ||
var oldShiftBlocks = ctx.shiftBlocks; | ||
var oldPush = ctx.push; | ||
ctx.shiftBlocks = function(locals) { | ||
return oldShiftBlocks.call(this, objMap(locals, function (l) { | ||
return wrapBlock(l, content); | ||
})); | ||
}; | ||
ctx.push = function(head, idx, len) { | ||
var newCtx = oldPush.apply(this, arguments); | ||
hackGibson(newCtx, content); | ||
return newCtx; | ||
}; | ||
} | ||
function wrapBlock(block, content) { | ||
return function (chunk, ctx) { | ||
ctx = ctx.push({intl: { messages: content }}); | ||
return block(chunk, ctx); | ||
} | ||
} | ||
function objMap(obj, fn) { | ||
var n = {}; | ||
Object.keys(obj).forEach(function (e) { | ||
n[e] = fn(obj[e]); | ||
}); | ||
return n; | ||
} | ||
module.exports.withLoader = module.exports; |
{ | ||
"name": "dust-usecontent-helper", | ||
"version": "4.0.0-0", | ||
"version": "4.0.0-1", | ||
"description": "A dust helper to load i18n content at render time", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,7 +15,11 @@ "use strict"; | ||
test('Does loader get called?', function (t) { | ||
test('Does loader work?', function (t) { | ||
require('../index')(function(context, bundle, cont) { | ||
t.same(context.options.option, true); | ||
t.pass('loader got called'); | ||
cont(null, {"hello": "world"}); | ||
if (bundle == 'test') { | ||
cont(null, {"hello": "world"}); | ||
} else { | ||
cont(null, {"hello": "this morning!"}); | ||
} | ||
}).registerWith(dustjs); | ||
@@ -25,8 +29,18 @@ | ||
dustjs.loadSource(dustjs.compile('{@useContent bundle="test"}{@message key="hello" /}{/useContent}', 'test')); | ||
dustjs.render('test', dustjs.context({}, { option: true }), function (err, out) { | ||
t.equal(out, "world"); | ||
t.pass("loader is called"); | ||
t.end(); | ||
t.test('does loader get called?', function (t) { | ||
dustjs.loadSource(dustjs.compile('{@useContent bundle="test"}{@message key="hello" /}{/useContent}', 'test')); | ||
dustjs.render('test', dustjs.context({}, { option: true }), function (err, out) { | ||
t.equal(out, "world"); | ||
t.pass("loader is called"); | ||
t.end(); | ||
}); | ||
}); | ||
t.test('do nested helpers work?', function (t) { | ||
dustjs.loadSource(dustjs.compile('{@useContent bundle="test"}{@useContent bundle="test2"}{@message key="hello" /}{/useContent}{/useContent}', 'nested')); | ||
dustjs.render('nested', dustjs.context({}, { option: true }), function (err, out) { | ||
t.equal(out, "this morning!"); | ||
t.end(); | ||
}); | ||
}); | ||
}); |
5290
92