-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Functions support #168
Comments
you should use helpers instead of passing in functions. |
It's very inconvenient. Actualy these "functions" are "properties". I.e. technically they are functions but only because of the lack of full properties support in all browsers. So it's logicaly to expect {{propName()}} syntax. So does paths: {{prop1().prop2()}}. |
I think this is by design, so I don't think it's going to happen. The goal for templates is to keep logic to a minimum, and helper functionality is available to assist with that. It seems like you are trying to set or read a property of a template context. You should be massaging your data before it goes into the template. |
anyway I'd like to hear the author. Honestly I don't understand your point about "keep logic to a minimum". I'm not trying to add any logic. I've just had my data in a form of objects with properties-functions. Take a look at Knockout (it's not my case but a similar approach). |
I've discussed this with @wycats and this is specifically something he wants to avoid. He's very adamant that Handlebars draw the line at string/number properties. If this is something you want support for, you'll have to override the compiler or write your own |
I understand you point. I'd allow writing templates with simple fields: p.s. I'd like also add that I can't customize HB with helpers for my case as HB evaluate helpers arguments before executing. For an example:
|
Excute my insistence but I've discovered that HB actually does support function calls. The only problem is HB passes some arguments with it (object like this: "{hash:{}}")
If I remove the "{hash:{}}" argument from function call then it starts working as I expect: put field name into template but have function "fieldName()" invocation at runtime. Could comment what "hash:{}" argument is and why is it needed. Maybe you'll find appropriate to remove it ? |
@evil-shrike I was thinking of this code in Ember: https://github.com/emberjs/ember.js/blob/master/packages/ember-handlebars/lib/ext.js#L78-101 which overwrites the default handler in Handlebars. It then calls this helper instead of the default one: https://github.com/emberjs/ember.js/blob/master/packages/ember-handlebars/lib/helpers/binding.js#L64-86 which either calls a helper if there is one of that name or sets up an Ember binding. As for the hash, I suspect that Handlebars treats any function it finds as a helper and the hash is passed as part of that. I don't see this behavior being changed. |
I've come up with the following solution:
It has pretty ugly part - "if (parent === 'helpers')", that's because of the fact that nameLookup is called for every name (even for helpers). |
I came across this as I am in a similar situation trying to use Handlebars with Backbone js. My models have lots properties I would like to execute at runtime and it is just very inconvenient to have a helper for each. @evil-shrike is your solution working as expected? |
@evil-shrike the code you provided above worked for me except for functions that required arguments. In backbone, a model's attributes are accessed via the
But it did not work. I didn't want to override toJSON so I created a new function to return the serialize object. |
Here's a better solution for Handlebars + Backbone integration. This checks for the existence of a get() method on the object and uses that for access if it exists.
|
Small change to the last one to add support for any function call on template, without argument(s) however:
Works with a models like this:
Note contains is not a core javascript list prototype function. Anyway in the template now you can use simple functions:
Keeps template logic simple. |
Let's consider the example:
This works. But instead of passing json-object I need to supply an object which has functions instead of fields:
So in template I'm trying to use a function call:
But this doesn't work. It throws:
What are my options?
The text was updated successfully, but these errors were encountered: