Forum Discussion

bryanpollack's avatar
11 years ago

Passing integers/booleans to a custom component

For some reason, I can pass an object variable or a hard-coded string to a custom component and retrieve it using env.context.component.getParameter, but when I try to pass an integer variable or a hard-coded boolean, it seems to error when it tries to retrieve the parameter and complains about a null value. 

This is my call:

 

<#assign numReplies = (thread.messages.linear.message?size - 1) />
<@component id="Message" message=starter numReplies=numReplies showSubject="true" />

 

And my retrieving code:

 

<#attempt>
<#assign showSubject = (env.context.component.getParameter('showSubject') == "true") />
<#recover>
<#assign showSubject = true />
</#attempt>
<#attempt>
<#assign numReplies = env.context.component.getParameter('numReplies') />
<#recover>
<#assign numReplies = 0 /> -- ends up going here because numReplies is null
</#attempt>

 

Thanks!

3 Replies

  • Haven't come across this issue before, but my guess would be that component parameters only accept certain data types. You could probably get around the issue by wrapping the values in a basic hashmap object.

    Have you tried something like this?

    <@component id="Message" message=starter customParams={"numReplies": numReplies, "showSubject": true} />


    <#attempt>
    <#assign showSubject = (env.context.component.getParameter('customParams').showSubject) />
    <#recover>
    <#assign showSubject = true />
    </#attempt>
    <#attempt>
    <#assign numReplies = env.context.component.getParameter('customParams').numReplies />
    <#recover>
    <#assign numReplies = 0 />
    </#attempt>

     

  • Hi Braynpollock,

     

    I think its mentioned in the  tkb post that parameters can only be of the type string. One of the workaround could be , for you to pass the message size as string , and then convert it to number using something like ,

    <#assign numReplies = (thread.messages.linear.message?size - 1) />

    <#assign numReplies = numReplies?string />

    <@component id="Message" message=starter numReplies=numReplies  showSubject="true" />

     

    and later in the component Message , 

    <#assign numReplies = env.context.component.getParameter('numReplies') />

    after the attempt part , when you are about to use the variable , you can convert it to number .

    <#assign numReplies = numReplies?number />

     

    Hope that helps .

     

    Thanks,

    Sam

     

     

  • I seem to get the same null/missing error when I try to pass the object. I tried assigning the object to a variable, and then accessing the object before I call the component, and that seems to work fine. Do you know if there's a way to create an XML object (or whatever kind of object is created by restadmin), as that seems to be the only kind of object I can pass, aside from a string.