From 2db266413f283e6072b250fcb0c9f70186d7fef8 Mon Sep 17 00:00:00 2001 From: "Ken N. March" Date: Tue, 13 Dec 2022 12:57:25 -0700 Subject: [PATCH 1/2] fix gastonrobledo/handlebars-async-helpers#11: support nested async values in context --- index.js | 10 ++++++++++ test/test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/index.js b/index.js index 364e65e..0bd4b21 100644 --- a/index.js +++ b/index.js @@ -52,10 +52,20 @@ function asyncHelpers(hbs) { return _escapeExpression(value) } + function lookupProperty(containerLookupProperty) { + return function(parent, propertyName) { + if (isPromise(parent)) { + return parent.then((p) => containerLookupProperty(p, propertyName)) + } + return containerLookupProperty(parent, propertyName) + } + } + handlebars.template = function(spec) { spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => { // const main = await spec.main container.escapeExpression = escapeExpression + container.lookupProperty = lookupProperty(container.lookupProperty) const v = spec.main(container, context, container.helpers, container.partials, data, blockParams, depths) return v } diff --git a/test/test.js b/test/test.js index 8115ca7..5124628 100644 --- a/test/test.js +++ b/test/test.js @@ -70,6 +70,32 @@ describe('Test async helpers', () => { }) + it ('Test lookupProperty', async() => { + const hbs = asyncHelpers(Handlebars), + template = `{{person.[0].firstName}}`, + expected = 'John' + const compiled = hbs.compile(template), + result = await compiled({ + person: [ + Promise.resolve({firstName: 'John', lastName: 'Q'}), + ] + }) + should.equal(result, expected) + }) + + it ('Test lookupProperty with nested promises', async() => { + const hbs = asyncHelpers(Handlebars), + template = `{{person.[0].firstName}}`, + expected = 'John' + const compiled = hbs.compile(template), + result = await compiled({ + person: Promise.all([ + Promise.resolve({firstName: 'John', lastName: 'Q'}), + ]) + }) + should.equal(result, expected) + }) + it('Test with helper', async () => { const hbs = asyncHelpers(Handlebars), template = `
From ce53d185385978c9db8b61f0f134209e59c60776 Mon Sep 17 00:00:00 2001 From: "Ken N. March" Date: Tue, 13 Dec 2022 13:15:31 -0700 Subject: [PATCH 2/2] fix whitespace --- test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 5124628..030c86a 100644 --- a/test/test.js +++ b/test/test.js @@ -70,7 +70,7 @@ describe('Test async helpers', () => { }) - it ('Test lookupProperty', async() => { + it('Test lookupProperty', async() => { const hbs = asyncHelpers(Handlebars), template = `{{person.[0].firstName}}`, expected = 'John' @@ -83,7 +83,7 @@ describe('Test async helpers', () => { should.equal(result, expected) }) - it ('Test lookupProperty with nested promises', async() => { + it('Test lookupProperty with nested promises', async() => { const hbs = asyncHelpers(Handlebars), template = `{{person.[0].firstName}}`, expected = 'John'