Skip to content

Commit

Permalink
Revert to pre-refactoring wrapper of the compile-function and `regi… (
Browse files Browse the repository at this point in the history
#15)

Fixes #14

- The refactoring changed to wrapper to be more explicit for readability. The result was
  that not all options were passed to the partial.
- The fix is to use the old wrapping mechanism for these function (i.e. "wrap(...)" which
  passes "all" parameters to the wrapped function.
  • Loading branch information
nknapp committed Jul 21, 2016
1 parent 612bd78 commit 022797e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,29 @@ function promisedHandlebars (Handlebars, options) {
var markers = null

// Wrap `registerHelper` with a custom function
engine.registerSyncHelper = engine.registerHelper
engine.registerHelper = function wrappedRegisterHelper (name, helper) {
if (typeof name === 'string' && typeof helper === 'function') {
engine.registerHelper = wrap(engine.registerHelper, function registerHelperWrapper (oldRegisterHelper, args) {
if (typeof args[0] === 'string' && typeof args[1] === 'function') {
var name = args[0]
var helper = args[1]
// Called like "registerHelper(name, helper)"
engine.registerSyncHelper(name, wrap(helper, helperWrapper))
} else if (typeof name === 'object' && typeof helper === 'undefined') {
oldRegisterHelper.call(engine, name, wrap(helper, helperWrapper))
} else if (args.length === 1 && typeof args[0] === 'object') {
// Called like "registerHelper({ name: helper })
engine.registerSyncHelper(mapValues(name, function (helper) {
oldRegisterHelper.call(engine, mapValues(args[0], function (helper) {
return wrap(helper, helperWrapper)
}))
}
}
})
// Re-register all built-in-helpers to ensure that their methods are wrapped
engine.registerHelper(engine.helpers)

// Wrap the `compile` function, so that it wraps the compiled-template
// with `prepareAndResolveMarkers`
engine.compileSync = engine.compile
engine.compile = function wrappedCompile (input, options) {
var fn = engine.compileSync(input, options)
return function (context) {
return prepareAndResolveMarkers(fn, [context])
}
}
engine.compile = wrap(engine.compile, function compileWrapper (oldCompile, args) {
var fn = oldCompile.apply(engine, args)
return wrap(fn, prepareAndResolveMarkers)
// Wrap the compiled function
})

/**
* Wrapper for templates, partials and block-helper callbacks
Expand Down
13 changes: 13 additions & 0 deletions test/main-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Handlebars.registerHelper({
return 'h(' + value + ')'
})
},
'stringifyRoot': function (options) {
return JSON.stringify(options.data.root)
},
'helper-hash': function (options) {
var hashString = Object.keys(options.hash).sort().map(function (key) {
return key + '=' + options.hash[key]
Expand Down Expand Up @@ -83,6 +86,7 @@ Handlebars.registerHelper('trim', function (options) {
Handlebars.registerPartial('a', "{{helper '10' 'partialA'}}")
Handlebars.registerPartial('b', "{{helper '10' 'partialB'}}")
Handlebars.registerPartial('identity', 'id({{.}})')
Handlebars.registerPartial('call', '{{{stringifyRoot}}}')

describe('promised-handlebars:', function () {
it('should return a promise for the ouput with helpers resolved', function (done) {
Expand Down Expand Up @@ -141,6 +145,15 @@ describe('promised-handlebars:', function () {
.notify(done)
})

it('partials calls should maintain the correct `root`-variable when a parameter is passed', function (done) {
var template = Handlebars.compile('{{>call a}}')
return expect(template({
a: 3
}))
.to.eventually.equal('{"a":3}')
.notify(done)
})

it('helpers passed into partials as parameters like {{>partial (helper 123)}} should be resolved within the helper call', function (done) {
var template = Handlebars.compile(fixture('helper-as-parameter-for-partial.hbs'))
return expect(template({}))
Expand Down

0 comments on commit 022797e

Please sign in to comment.