views:

89

answers:

1

I need to be able to add some javascript to all ajax postback responses (PartialViewContext.isAjaxRequest == true) but I am not succeeding with any implementation I try.

I have tried implementing a

PhaseListener

and adding my script using PartialResponseWriter.insert* to add eval blocks, as well as trying to add the script by creating a script element. (Results in CDATA cannot nest, or just invalid XML)

I have tried decorating PartialViewContextFactory to override the

PartialViewContext.processPartial

and add the script after the wrapped instance has processed it...

How should I go about adding sripts to an Ajax response? Something similar to what .NET has with Scriptmanager.registerClientScriptBlock preferably.

Thank you

A: 

Use the javascript libraries that come bundled with your JSF2 implementation (as required by the spec). Here are the API docs:

http://java.sun.com/javaee/javaserverfaces/2.0/docs/js-api/index.html

The jsf.ajax.addOnEvent solves your problem of "adding some javascript to all ajax postback responses":

This function must accept a reference to an existing JavaScript function. The JavaScript function reference must be added to a list of callbacks, making it possible to register more than one callback by invoking jsf.ajax.addOnEvent more than once.

Brian Leathem
Thank you for the response, but that suggestion only works if all postbacks to the server go through the JSF client library code, and you have access to the client code. I am looking for a solution on the server side, with an approach similar to what is available in the .NET platform with ScriptManager.
CDSO1