The only answer I got was this:
The short answer: NO.
The long: it is somewhere on the list.
I don't like those kinds of answers.
After todays experiment, I can firmly say: Yes, it's possible!
This is what you'll be able to do with the simple framework provided in the demo:
- Dynamically bind/unbind custom events
- Bind multiple event handlers to one event
- Run the event in a specified scope
- Use regular HTML elements as event triggers
This started out as an experiment into Server Side Event Delegation. A couple of nights later, I further improved on the functionality.
In the slumbering stage this morning, I discovered that I was on the path for something, but not what I initially thought I had started out for. The two first experiments lead to what would have been a complex unusable blob of functionality.
Today, I did some cleanup. I removed all the class attributes, and rewrote the framework code to be more flexible. The previous two version relied on an eventHandler node in the top of the XPage, which results in a generated script tag. This implementation doesn't add any script tags to the generated html.
The server side part of the code lets you specify what to do for certain events. The client side part of the code specifies what server side event to run, what data to send to the event handler (optional), and which part of the page should be updated after the event has run.
I did a little screen capture of the demoapp in action (not visible in Google Reader):
Simple Server Side Event Delegation:
The two first buttons toggles (when active) visibility of a couple of computed fields. The third button binds/unbinds the event handler for the two preceding buttons.
Multiple Event Handlers:
The first button triggers a custom event on the server. The second button adds an event handler to the event. As you can see from the demo, for each event handler added, the addition increases by 1.
Event handler running in a specified scope:
The event handler for the event runs in the scope of an object on the server. Then the event is triggered, a viewScope variable is created from a property fetched from the object ( viewScope.put( 'theAnswer', this.theAnswer ) )
In View Edit:
When the name of the person is clicked, an "edit" event is triggered. When the ok button is clicked, a "save" event is triggered.
If you add the event handlers afterRestoreView or before-/afterRendreResponse, an event handler is added for every partial refresh. Adding them in an event handler or in the afterPageLoad event should be safe.
>> Download Demoapp
If you don't like a lot of generated server side script tags for events/embedding the event code in control after control, this is definitely worth looking at.
If you find any bugs/want more info about something, leave me a comment.
Share and enjoy!