Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handlebars does not support class instances in the template data #2043

Open
75lb opened this issue Sep 8, 2024 · 0 comments
Open

Handlebars does not support class instances in the template data #2043

75lb opened this issue Sep 8, 2024 · 0 comments
Labels

Comments

@75lb
Copy link

75lb commented Sep 8, 2024

When rendering a compiled template with an array of custom class instances, this line butchers the input class instance replacing it with a plain object with all custom class behaviour (getters, setters, methods etc) removed. This breaks my app.

Please remove all destructive use of Util.extends() on the input data - leave the input data as it is.

Some pseudocode to illustrate my case. Please let me know if you need anything else.

class Car {
  wheels = []
  maxSpeed
  colour

  constructor (data) {
    this.maxSpeed = data.maxSpeed
    this.colour = data.colour
  }

  hasFourWheels () {}
}

const cars = [
  new Car({ maxSpeed: 40, colour: 'silver' }),
  new Car({ maxSpeed: 30, colour: 'white' })
]

handlebars.registerHelper({
  customHelper: function () {
    /* Fails as the input data is no longer a `Car` instance */
    if (this.hasFourWheels()) {
      // do something
    }
  }
})

/* myTemplate includes use of the `customHelper` helper. */
const compiled = handlebars.compile(myTemplate)
/* throws an error */
compiled(cars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants