Friday, August 26, 2011

XPages: Passing event handler code to custom control

Update: After a little test, it looks like the onchange event of the combo fires for every refresh. I'll move the code to beforeRenderResponse or something like that instead.

In an application I'm currently working on, I have a combobox that's used in several pages. The values the combobox contains persist over every page, but what happens when the user changes value varies from page to page.

I saw that the combobox has several properties for events under all properties. I tried adding
#{compositeData.onchange} to the onchange event, and it works. One caveat is that it seems to fire three times, but I can live with that.

To implement:
Add custom properties to the custom control for the events you want to have custom event handlers for. In the source code of the field, add attributes for the events you want code to run. E.g. onchange="#{compositeData.onchange}"

In the XPage under custom properties for the custom control, write the SSJS you want to run for your events.

That's about it.

I have only tested this with ComboBoxes, but I'm not surprised if it works for most fields that have event properties.

Tested on server running Domino 8.5.2 FP2

3 comments:

serdar said...

Tommy,

I blogged about it a while ago.

http://lotusnotus.com/lotusnotus_en.nsf/dx/lesson-of-the-day-i-need-to-learn-more-about-xpages.htm

You may define the parameter like;

return function handler() {blah blah }

it would work then. It's not a cool solution but works.

Tommy Valand said...

I considered that approach after I saw what happened. The code has to run in the scope of a document data source in a repeat control, so if nothing else works, I'll have to get dirty :S

Tommy Valand said...

I found a way to implement the "hack" in a way that hopefully readable for future developers of my applications.

I use a Fn postfix on the custom property, and in Description, "Function...".

E.g.
onchangeFn
"Function to run in the onchange event"