JSF 2 Scriptmanager style functionality - ajax

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

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.

Related

Is there a wait/sleep function in Freemarker?

I tried to find a wait/sleep/pause function in freemarker, but couldn't find anything on page 1 of Google or the Freemarker documentation. Does anyone know if this exists, or if there's a workaround that can be used?
FreeMarker doesn't have wait function, FreeMarker is a server-side compiled UI templating language which means the code you write gets compiled and the result is sent to the UI.
See documentation for more: https://freemarker.apache.org/
If you want to have sleep then considers javascript setTimeout() function or jQuery's sleep function. You cam mix javascript/jQuery easily with FreeMarker.

Does successful form elements are submited as parameters automatically on AJAX calls?

Do we have to include all form elements (ids and values) manually in the query string when making an Ajax call, or they are automatically (according to standard) included as parameters to the query string of the request?
Well, since no one is eager to answer my question. I will.
the quick answer is no. by standard let's say HTML standard. form element values are not automatically submitted a long with a request as parameters, when making an Ajax call.
but when you submit a form by submit() function of the form element, it will include all successful elements in the query and will send it as parameters to the server.
so If you are using Ajax, you have to include every elements which you need yourself.
however there are some utility functions that can help you. like the serialize() method in Jquery framework.
have fun :)

How to override Dojo's xhrGet and xhrPost?

We are extensively using Dojo's xhrGet and xhrPost in our application. This has been used across multiple JavaScript files. Now we need a uniform way in which we handle the exceptions that are returned from the server in case of an AJAX call. We don't want to handle this in all places where we are using Dojo's xhrGet or xhrPost. Is it possible to do that without disturbing any of the existing code? For example, when some exception is sent from the server as part of the ajax response, I need to display some message in a consistent way across the application.
Could you please suggest me a solution for this? Kindly let me know if any more information is required.
Use IO Pipeline Topics as I described in Generic Loading Icon in Dojo and you won't have to change your code at all.
did you look at the dojo/aspect or dojo/on ? You can define functions that get executed after a function was called (or before) with aspect.
Take a look at that:
http://dojotoolkit.org/reference-guide/1.8/dojo/aspect.html#dojo-aspect-after
Why dont you create a custom xhrArgs class using dojo/declare that has the same error function for all his children ?
http://dojotoolkit.org/reference-guide/1.8/dojo/_base/declare.html#dojo-base-declare
Lucian

What is the canonical way to handle errors during Ajax-requests?

For normal requests we can simple register an <error-page> in web.xml. However, this does not apply to Ajax-requests. By default errors during an Ajax-request will result in a little pop-window in the browser that shows the exception.
The main example I am struggling with is handling a ViewExpiredException in a uniform way. For standard requests, I redirect to a page that explains that the user is not logged in and provides a link to the login-page. I would like to do the same for Ajax-requests. There seem to be several ways:
I could write a javascript function that handles the error on the client-side and redirects to the error-page. I would then have to add this function every <f:ajax>-tag on all pages using the onerror-attribute. Is there a way to tell JSF that I want to have this javascript-function as the default error-handler for all <f:ajax>-tags?
I could use a custom exception-handler, as described in this blog. This seems to do what I want, but I wonder if it is overkill. Is there no simpler solution?
So my question is, how is this supposed to be solved? Which of the approaches I listed should be used? Is there another approach that I do not know of?
You can use jsf.ajax.addOnError() to set the default error handler. E.g.
jsf.ajax.addOnError(function(data) {
alert(data.responseText);
});
See also chapter 13.3.6.2 of the JSF2 spec. You can find all properties of data object in table 14-4 of the JSF2 spec.

Is it possible to send a form and a html request with same Event by Mootools?

$('submitbutton').addEvent( 'submit', function(e){
e.stop();
$('fuss').send();
req2.send();
});
trying to get this working but not sure if it is possible and had no success so far.
Mootools docs doesnt helped me either.
Will the multiple usage of .send() work?
Do i have to specify the data beeing send for the html request or does it take automatical the data beeing send by the form ?
It's in the documentation: http://mootools.net/docs/core/Request/Request#Element:send
This shorthand method will do a request using the post-processed data from all of the fields.
You can do as many requests in a specific event as you wish, as long as they're all asynchronous.
In your specific example it would seem you want to do two requests using two different methods, one with setting up a new Request class manually and the second doing it via the Element Method.
Based on your last comment, I wrote a little example in jsFiddle.
Basically I think you don't need two request for your goal. Just override onRequest method to update the html.

Resources