I am having an issue where sometimes the object I want to iterate through is just a plain object but sometimes it's an array, for both I am using the {{#each}} Handlebars built-in helper.
When the object is an array, it works just fine, but when it is a plain object it is not working. How can I get around that?
Thanks!
when iterating over properties {{#key}} refers to property name, and {{this}} refers to property value
more here
https://handlebarsjs.com/guide/builtin-helpers.html#each
Related
I would like to pass a partial a variable that contains a helper, so that the partial can better reused, However, my companies installed version of handlebars will not accept new helpers.
data eg:
{
"orderLines":[
{"unitPrice": "0.46","isItemUnavailable": "Y"},
{"unitPrice": "0.46"}]
}
Template 1:
{{> myPartial helpername="if" totest="isItemUnavailable"}}
Inside myPartial:
{{#each orderLines}}
{{#helpername totest}}
// stuff here if isItemUnavialable
{{/helper}}
{{/each}}
The problem as I see it, is that I cannot pass a variable to my partial in order to dynamically set a helper?
The outcome of the above is an error in the partial, saying it cannot find helper "helpername" even though it is supposed to render "if"
The partial passes the variable if. But when you place it beside a hash, it no longer accepts it as a variable with a value, but looks for #helpername as a registered helper, which then throws an error because there isn't one.
I am creating a custom helper.
If I use my new helper within an object like this:
{{#data}}
{{newHelper}}
{{/data}}
How do I access the data object from my helper function?
I know I can do
args.data.root['data']
But I want to access it dynamically because it wont always be within an object called 'data', it could be anything.
You can access the current context with this
So instead of using args.data.root['data'].value you can simply use this.value
This may seem like a simple question, but it is one that I have struggled to find documentation for.
I have a spacebars helper that returns values from a collection, in a cursor of objects for use in an {{#each}} block. These objects have a boolean property that I use to check/uncheck a checkbox.
However, the boolean values in the database need to be inverted for use in the checkbox. If a record in the collection has the boolean property evaluating to "false," I need it to be "true" in usage.
{{#each records}}
{{name}}: <input type="checkbox" checked="{{!checked}}">
{{/each}}
The issue here is that {{! signals a spacebars comment, rather than converting "false" to "true."
In this snippet, {{!checked}} is considered a comment rather than a helper.
Theoretically, I could run a forEach() loop in the helper logic and invert the boolean values for each object. However, I feel like there must be a better way for something as simple as this.
Just make yourself a not global helper:
Template.registerHelper('not',(param)=>{
return !param;
});
Then in any template use {{not checked}}
Struts2 inserts java.lang.Strings into Maps as default, but... is there a way to override that behavior for some parameters and insert different Objects using many custom Type Converters?
For example, in a Java class we can declare a HashMap and put something like:
myHashMap.put(“name”,”myName”); //this is a String
myHashMap.put(“id”,new Integer(“101”)); //this is an Integer
myHashMap.put(“date”,java.util.Calendar.getInstance().getTime()); //this is a Date
Is it possible to assign the correct java.lang.Object directly from the <s:form> using Type Converters?
If you have something like this in your <s:form>:
<s:textfield name="myHashMap['name']"/>
<s:textfield name="myHashMap['id']"/>
<s:textfield name="myHashMap['date']"/>
Every value ends as a java.lang.String inside the HashMap, instead of having a String, an Integer an a Date...
I tried to create my own Type Converter with no luck... I guess Struts2 wants a POJO with setters and getters for each parameter, but the HashMap uses the “put(Key,Value)” method.
I have the setters and getters for “myHashMap”, but I thought Struts would somehow use it like a POJO when setting the parameters (when calling “put(Key,Value)”).
I created “MyAction-conversion.properties” file and wrote this:
date=app.converter.MyDateConverter
It didn't work... Then I tried also doing this:
myHashMap['date']=app.converter.MyDateConverter # (didn't work)
myHashMap.date=app.converter.MyDateConverter # (didn't work)
The converter isn't called at all! I do have “MyDateConverter” class and it is working fine. If I use a POJO (instead of the HashMap) and create the setters and getters inside the POJO for “name”, “id” and “date” it works great. But the thing is that I want to use something more generic, like a HashMap, in order to change the name of the parameters in the form or add more without having to create another POJO.
For the moment, it works using the HashMap if you expect to receive only Strings, but I don't know how to call a Type Converter to receive custom Objects. The converter isn't called and I end always with Strings.
struts2 can convert java.util.Date from String without coding your converter. Infact,'date' is String type ,struts can not find it by it's content but type.
Doubtful, and if you can, don't. Doing so would require the use of a raw map and casting to retrieve the values. Type safety is your friend.
But the thing is that I want to use something more generic, like a HashMap, in order to change the name of the parameters in the form or add more without having to create another POJO.
That isn't a better design. Stick with the POJO approach.
When writing code in X++ you sometimes need to reference this.functionYouWant() and sometimes it is element.FunctionYouWant(). Sometimes both are in scope. I often try one and if the function I want isn't there I try the other. Is there a rule that explains when to use this and when to use element?
this can be used in any objects to reference the current object and member methods.
MorphX forms and reports are composite objects.
In forms the collection of objects is contained within a FormRun object. You can reference members in the outer FormRun object by using the element reference.
If your code is placed at the top level there are no functional difference between this and element.
If your code is placed in a FormDataSource this will reference the datasource but element will reference the FormRun.
"This" can be used only refers to the same class objects but "element" is access the form methods into any form level(like datasouce level,design level)if the functionality of method is same we can use