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

ES6 Getters/Setters support #79

Open
imajus opened this issue May 27, 2017 · 4 comments
Open

ES6 Getters/Setters support #79

imajus opened this issue May 27, 2017 · 4 comments

Comments

@imajus
Copy link

imajus commented May 27, 2017

I had to use get/set ES6 syntax in helpers but plugin didn't support those. Here's a patch I suggest to apply in order to support getters/setters in helpers:

_.each(Object.getOwnPropertyDescriptors(helpers), function(descriptor, key) {
  Object.defineProperty(self._helpers.prototype, key, descriptor);
});

The patch is backward compatible with old good helper methods.
With that change the following helpers start working:

Orders.helpers({
  // Still works this way
  url() {
    return FlowRouter('order', this);
  },
  // Getters…
  get product() {
    return Products.findOne(this.productId);
  },
  get total() {
    return this.quantity * this.price;
  }
 //TODO: Never actually tested Setters yet
});
@dburles
Copy link
Owner

dburles commented May 29, 2017

Hey @imajus interesting thought, wondering what was the reason that you said you had to use them?

@imajus
Copy link
Author

imajus commented May 29, 2017

@dburles Using Collection instances in Handlebars E-mail templates on the server. So I need {{order.total}} to work seamlessly. Unlike Spacebars, functions here are called with current template context so this in Collection helpers is not Collection instance.
I've found using getters as an elegant way to achieve my goal.

@medihack
Copy link

medihack commented Dec 10, 2017

I absolutely agree that this would be very useful, especially for computed properties.

Authors.helpers({
  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
});

author.fullName would now work like author.firstName, as it is supposed to be.

And I can confirm that the above fix by @imajus (thanks a lot!) works correctly (using it myself).

@arvidkastel
Copy link

Seems great! How can i use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants