Repository navigation
fix(runtime): keep class instances intact when partial is invoked with a hash - #2201
Open
cdytotoro-ctrl wants to merge 1 commit into
Conversation
…h a hash
Invoking a partial with a hash flattened the context with
Utils.extend({}, context, options.hash), replacing class instances with
plain objects. Helpers inside the partial then received a context
without any prototype methods or getters, breaking templates rendered
over class-based models (Fixes handlebars-lang#2043).
The merged context now keeps the original prototype for class instances
(any object whose prototype is neither Object.prototype nor null and
which is not an array or a map), so methods and getters still work
through 'this' while hash values continue to override context
properties. Plain objects, arrays, maps and primitives keep the
previous flat-copy behavior.
Fixes handlebars-lang#2043
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The context-copying change causes hashed partials to throw for Set and Date contexts and prevents reliable hash overrides of inherited accessors or non-writable properties.
2 open issues
What changed in this PR
This PR addresses #2043 by preserving class-instance prototypes when merging hash arguments into partial contexts, allowing helpers to access model methods.
Changes:
- Adds prototype-preserving context merging for class instances while retaining flat-copy behavior for plain objects, arrays, maps, and primitives.
- Adds tests for helper method calls,
instanceofchecks, and hash overrides.
| File | Description |
|---|---|
spec/partials.js |
Adds class-instance and hash-override regression tests. |
lib/handlebars/runtime.js |
Introduces prototype-preserving merging for partial hash arguments. |
Review effort: Balanced
| typeof context === 'object' && | ||
| context !== null && | ||
| !Utils.isArray(context) && | ||
| !Utils.isMap(context) |
Comment on lines
+31
to
+32
| Utils.extend(instance, context); | ||
| Utils.extend(instance, hash); |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Invoking a partial with a hash flattened the context via Utils.extend({}, context, options.hash), replacing class instances with plain objects. Helpers inside the partial then received a context without prototype methods or getters, breaking templates rendered over class-based models (Fixes #2043).
The merged context now keeps the original prototype for class instances (objects whose prototype is neither Object.prototype nor null and which are not arrays or maps), so methods and getters still work through 'this' while hash values continue to override context properties. Plain objects, arrays, maps and primitives keep the previous flat-copy behavior, and prototype access control gating is unchanged.
Tests: new cases in spec/partials.js verify helper method calls and instanceof checks inside hashed partials, plus hash override semantics for class instances.