The onsubmit is a simple callback coming from the dom tree, it is not executed during ajax because there is no form submit performed.

Have in mind on some browsers you dont even get the event with special dom configurations (aka, simple form posts not by submit buttons etc...) or the event cannot be blocked, so do not entirely rely on this event, it is flakey.

For the ajax case:

You can use the global listeners however, set yourself a marker class or an attribute in your form element which should trigger the listener and if you get an ajax request you can check for this marker whether you should intercept with your code or not.

Here is a short pseudo code example:
function submitHandler(data) {
        if(data.status == "begin") {
                var src = data.source;
                
                var form = getParentForm(src);
                if(hasClass(form, "myMarker")) {
                        intercept();
                }
        }
}


You cannot cancel the request that way, but at least you have a callback. Practically by throwing an error in your code the request then will be cancelled and an error handler will be called.
That might be a dirty way to cancel the request upfront.




Am 06.02.12 14:12, schrieb Michael Heinen:
Thanks Werner for the suggestion.

I created meanwhile a JIRA issue for this:
https://issues.apache.org/jira/browse/MYFACES-3460

A global js listener is unfortunately not what I need.
In my case it depends on the form, whether a js should be executed
during submission or not.
Some requests can run in parallel (e.g. autocompletion, pulls) while
others are blocking.

Do you know why the simple onsubmit of a form is not executed for ajax
requests?

Michael

Am 06.02.2012 14:02, schrieb Werner Punz:
Hi if you need to execute commands before any arbitrary ajax call
then you can add your own global ajax request listener via
jsf.ajax.addOnEvent(callback)


The callback is called three times per request, same as if you would
set it directly within the request, but on a global scale.

http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/js-api/symbols/jsf.ajax.html


Unfortunately a deregistration is not possible within the jsf api
within myfaces there is a way but that would break the compatibility
of the code with Mojarra.


Werner





Am 02.02.12 11:05, schrieb Michael Heinen:
Hi,

I am currently migrating an application to JSF 2.1

I have a lot of ajax commands (richfaces) and some new f:ajax tags.
My forms contain some js functions which should be called during
onsubmit but unfortunately onsubmit is not called for ajax requests.

How can I specify some JS functions that should be executed before any
ajax call?
I do not want to add them to a few hundred commands manually and would
like to define the calls on a few spots as possible.

Thanks,
Michael








Reply via email to