For a long time, every time I wanted to create a controller or model bean, I wrote it and then added it to my managed beans in faces-config. But this can create a very bloated faces config, particularly when these beans are only used by a single XPage. Then I learned about dataContext.
A dataContext can be created for any container all the way up to the xp:view element, which allows for great flexibility. It works like this:
<xp:this.dataContexts>
<xp:dataContext var="beanVar">
<xp:this.value><![CDATA[${javascript:
var unid = param.get("unid");
var notice:package.Notice = new package.Notice(unid);
return notice;}]]></xp:this.value>
</xp:dataContext>
</xp:this.dataContexts>
This bean can then be bound to controls like so:
<xp:text id="name" value="#{beanVar.name}"></xp:text>
If the bean returns a collection, this can be used as a data source for a repeat control like so:
<xp:repeat id="repeatItems" value="#{beanVar.itemList}" var="item" rows="50">
<div class="row>
<xp:text id="itemName" value="#{item.name}">
<xp:text id="itemQty" value="#{item.quantity}">
</div>
</xp:repeat>
So it works just like a managed bean except it is declared in the XPage instead of faces-config. This has the added benefit of making it clearer where the bean variable name is coming from when you are looking at the XPage in the future.
A dataContext can be created for any container all the way up to the xp:view element, which allows for great flexibility. It works like this:
<xp:this.dataContexts>
<xp:dataContext var="beanVar">
<xp:this.value><![CDATA[${javascript:
var unid = param.get("unid");
var notice:package.Notice = new package.Notice(unid);
return notice;}]]></xp:this.value>
</xp:dataContext>
</xp:this.dataContexts>
This bean can then be bound to controls like so:
<xp:text id="name" value="#{beanVar.name}"></xp:text>
If the bean returns a collection, this can be used as a data source for a repeat control like so:
<xp:repeat id="repeatItems" value="#{beanVar.itemList}" var="item" rows="50">
<div class="row>
<xp:text id="itemName" value="#{item.name}">
<xp:text id="itemQty" value="#{item.quantity}">
</div>
</xp:repeat>
So it works just like a managed bean except it is declared in the XPage instead of faces-config. This has the added benefit of making it clearer where the bean variable name is coming from when you are looking at the XPage in the future.
Comments
Post a Comment