Reputation: 2087
I have a fairly complex Custom Control that may be used multiple times on any given XPage. In the control I create a couple of viewScope variables that have to be unique to the specific custom Control. I would like to do something like viewScope.put(customControlID + "variableName","Stuff) But I don't know how to get the custom controls ID
Upvotes: 3
Views: 1439
Reputation: 10485
You can use a dataContext variable:
<xp:this.dataContexts>
<xp:dataContext
value="#{javascript:this.getId()}"
var="id">
</xp:dataContext>
</xp:this.dataContexts>
The variable is then accessible as id in SSJS...
<xp:label id="label1" value="#{javascript:id}" />
... or in EL:
<xp:label id="label1" value="#{id}" />
Upvotes: 3
Reputation: 10485
Here is another solution as an SSJS function:
function getCCId( cmp:javax.faces.component.UIComponent):string{
try{
if( typeof( cmp ) === 'com.ibm.xsp.component.UIIncludeComposite' ){
return cmp.getId();
}
return getCCId( cmp.getParent() )
}catch(e){}
}
The function climbs the component tree until it finds the parent CC and then returns the id.
You can use it f.e. in a label like this:
<xp:label id="label1">
<xp:this.value><![CDATA[#{javascript:getCCId( this )}]]></xp:this.value>
</xp:label>
Upvotes: 1
Reputation: 30960
You can get the current custom control ID with this.getId()
at the <xp:view>
level.
If you put this ID into a compositeData variable (e.g. compositeData.id
) then you can use the ID inside the custom control everywhere you want.
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
beforePageLoad="#{javascript:compositeData.id = this.getId()}" >
Usage in SSJS:
viewScope.put(compositeData.id + "variableName","Stuff")
Typically, IDs are named like "_id2", "_id8", ...
Upvotes: 11