Subscribe via Feed

Adding the Ability to Watch for ANY Partial Refresh in an XPage

Jeremy Hodge, Oct 28, 2009 1:18:03 PM

I was recently asked the question if it is possible to get an onComplete event to run after a partial refresh that has been executed by the pager for a View control. The answer is yes, but its not as simple as creating the onComplete event for the pager control, it doesn't really exist (at least not through the UI, and not without hacking and creating your own code for attachPartial, etc.)

However, what we can do, is add a simple script library to the page that will "watch" for ANY partial refresh that occurs on a page, and if we detect the partial event executes for an ID we want to do something for, we can inject the onComplete.

So here's the secret, we're going to override some methods in the XSP object to use dojo's topic publish and subscribe system to "broadcast" when a partial refresh event occurs.

First, create a javascript library, and place this code in the library:

 

XSP._inheritedPartialRefresh = XSP._partialRefresh;
XSP._partialRefresh = function(method,form,refreshId,options){
    dojo.publish("/XSP/partialRefresh", new Array(method,form,refreshId,options));
    this._inheritedPartialRefresh(method,form,refreshId,options);
}

 

What we have done here is create a copy of the XSP's internal function _partialRefresh, named _inheritedPartialRefresh. This will allow us to call the original function later.

Next, we re-write the XSP object's _partialRefresh function. The first line of the new function just "publishes" an event called "/XSP/partialRefresh" to the topic system, alertiing anybody that "subscribed" to it, that the event has just occured. The next parameter is an array of parameters that we are going to pass to the subscribed event handler(s). What we'll pass is the four parameters that are going to be passed to the original _partialRefresh function.

During the "publish", the dojo Topic system then calls each event handler that has subscribed to this topic. Because the parameters are passed to the subscriber, they can modify them. So, in our XPage, we can add the following code to "subscribe" to the new partialRefresh event, like this:

 

dojo.subscribe("/XSP/partialRefresh", null, function(method,form,refreshId,options) {
    alert('You just partial refreshed "' + refreshId + '"!! Injecting new onComplete');
    if (options.onComplete)
        options._inheritedOnComplete = options.onComplete;
    
    options.onComplete = function() {
        alert('Youve just been injected');
        if (this.inheritedOnComplete)
            this.inheritedOnComplete();
    }
});

 

Here, we basically are telling dojo, when the "/XSP/partialRefresh" event is published, please call my function. In this sample function, I pop an alert that says the partial refresh just got called, then I modify the options object that holds the onComplete object. I check to see if the onComplete event already exists, and if it does, I create a copy, then create a new onComplete, that executes my code, then calls the old onComplete.

Just a few notes: you can subscribe to the same Topic ("/XSP/partialRefresh") multiple times .. however you have to make sure your code is safe. For example, my code above is not safe if I used the same code to override the onComplete in each subscribe, because on the second subscribe, the original onComplete would get destroyed, but I think I've given you enough of a base to get started with!

You can see an example of this at work here.

Happy Coding!



2 responses to Adding the Ability to Watch for ANY Partial Refresh in an XPage

Jeremy Hodge, April 18, 2010 12:12 PM

I would recommend you look at this more recent post:

http://xpagesblog.com/xpages-blog/2010/3/25/reusable-javascript-class-to-publish-global-and-element-spec.html

It has a re-usable javascript class that publishes general and element specific partial refresh events for the entire partial refresh, and then one for each of the processing events (onStart, onComplete, onError) ... with that you can subscribe safely to each of these events multiple times, and do not have to inject your own onComplete.

Thanks

Jeremy


JJTB Somhorst, April 18, 2010 7:32 AM

Good post on how to react on the partial events. Could you post some 'safe' code ?