XPages – using # and $ at the same time

Paul Withers posted a great about the XPage Binding and how the order of computing for $(..) and #(..) means that the following code does not compute as we would want it to. The article is well explained and opened my eyes to the intricacies of the timing in JSF. It got me thinking about it and I think I have a solution.


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:text escape="true" id="computedField1"
value="This field's value is#id{id:computedField1}">
</xp:text>
<xp:br></xp:br>
<xp:text escape="true" id="computedField2"
value="You are logged in as ${javascript:@UserName()}. This field's value is #{id:computedField1}">
</xp:text>
</xp:view>

output:
This field’s value is#id{id:computedField1}
You are logged in as CN=Mark Roden/O=MAXDEV. This field’s value is

Solution
The solution appears to be forcing the #{id:computedField2} to compute using SSJS @Text – but I am not sure why this works and doesn’t fail like the original example.

$(#{id:computedField2})
is replaced with
${javascript:@Text(“#{id:computedField2}”)

I used <xp:value> in this example for clarity because of the extra ” in the @Text formula


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:text escape="true" id="computedField1" value="This field's value is #{id:computedField1}">
</xp:text>
 <xp:br></xp:br>
 <xp:text escape="true" id="computedField2">
 <xp:this.value>You are logged in as ${javascript:@UserName()}.
 This field's value is ${javascript:@Text("#{id:computedField2}")}
 </xp:this.value>
 </xp:text>
</xp:view>

output:
This field’s value is view:_id1:computedField1
You are logged in as CN=Mark Roden/O=MAXDEV. This field’s value is view:_id1:computedField2

UPDATE (02/13/2012)

Sven’s Right – unfortunately this code is not JSF compliant and is more likely to be a bug in Notes which will be corrected in a later version.

Paul came up with a much more elegant solution using dataContexts

Glad to be part of the discussion 🙂