Saturday, February 27, 2010

XPages: Use converters to emulate domino computed fields

I don't have much time to write this (people to meet, stuff to do), so I'll keep it short.

One of the things I really missed going from standard Domino development to XPages development is the (Domino) computed fields.

When I woke up this morning, I thought "what about converters?". Tested it out, and it works! :)

In the getAsObject-code (all properties), you write the code that computes the field.
var numItems = getComponent( 'numItems' ).value;
var price = getComponent( 'price' ).value;
return (numItems && price) ? numItems * price : 0;
The above example stores the value as number, so in getAsString, you have to convert the number to string (value is a local variable containing the value of the field):
I didn't have much time to test, so I don't know all the weaknesses with this technique. One thing I found. If the field is set to read only, the converter is ignored/the value is static.

The technique should work great for hidden fields that have dynamic values. It lets you have all the computing code relevant to the field in the field (instead of setting the field form other fields' events, or in XPages events).

If you can think of other ways to use this technique, please leave a comment.


NotesSensei said...

really neat. Keeping the code with the field makes maintenance so much easier