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 🙂

3 thoughts on “XPages – using # and $ at the same time

  1. Hi,

    if you are changing your code above to

    ${javascript:@Text(#{id:computedField2})}

    an error will occur. This is correct, because the @Text-Formular is empty – the id is not available during on page load).
    It seems that the different behaviour is caused by using code INSIDE the aposthropes.

    Sven

    • Yeah I saw that as I was playing with it. It was really a trial and error effort which came up with the solution. I would love to claim some understanding but I can’t really.

      I really appreciate the discussion though – fascinating to see how things work under the cover, bug OR feature 😀

      Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s