handlebars-lang / handlebars-lang/handlebars.js

functions in nested objects are not bound to that object, resulting in inconsistent behavior

Open
#1,860 6 comments 0 reactions 0 assignees View on GitHub
docs-needed item for language spec needs investigation
Dominant language
JavaScript
Stars
18.7k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

Given the following input:
``` javascript
const context = {
nested: {
awesome: function () {
return this.more;
},
more: 'Deeply awesome'
},
more: 'More awesome'
}
```

the following template:
``` handlebars
{{nested.awesome}}
```
yields `'More awesome'`, whereas the equivalent Javascript:

``` javascript
context.nested.awesome()
```
yields `'Deeply awesome'`

More importantly,

```
{{#with nested}}
{{awesome}}
{{/with}}
```
also yields `'Deeply awesome'`.

Here is the [js-fiddle demonstrating this](https://jsfiddle.net/02rxztk5/)

PR submitted with a failing test case: #1861.

Contributor guide

Open the contributing guide

Research direction

Start with the nested-object examples in the issue and the failing test case submitted in PR #1861. Reproduce the difference between {{nested.awesome}} and the #with form, then confirm that direct property access binds the function to the nested object and returns 'Deeply awesome'.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.