-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ajax b:commandButton loses AJAX if onclick is used #750
Comments
I thought this is the expected behavior, but after checking what |
When I first saw it I couldn't classify it as a bug. But I didn't see it natural. If I set ajax to true, I expect the form to be submitted via ajax, independently of any other events. |
I frequently to something like |
This is causing the following issues:
The workaround is manually making the AJAX request on the onclick using BootsFaces APIs, like if it was rendered from the |
IMHO we should chain the JavaScript and the AJAX call, just like Mojarra, MyFaces and PrimeFaces do it. We just haven't started to do so :). |
+1 |
1 similar comment
+1 |
I also stumbled on this bug and it was not clear to me. I first used |
For clarification: <!-- uses ajax as expected -->
<b:commandButton action="#{myBean.doSomething}" ajax="true" />
<!-- Does NOT use ajax, but should -->
<b:commandButton action="#{myBean.doSomething}" ajax="true" onclick="console.log('wtf')"/>
<!-- Workaround until this is fixed -->
<b:commandButton onclick="console.log('wtf');ajax:myBean.doSomething()"/> |
I see one thing here worth checking in @t-oster 's example above <!-- Does NOT use ajax, but should -->
<b:commandButton action="#{myBean.doSomething}" ajax="true" onclick="console.log('wtf')"/> It is not clear whether |
I agree that the workaround is easier to understand. That's why I invented |
When using ajax in the onclick listener, I agree it is not clear, however if just using plain JS I would have assumed that it behaves like the usual on click handler,meaning the JS is evaluated first and after that the usual action is performed (as it already works with ajax=false) |
What is the state of this issue? Is there any reason not to chain the user submittet 'onclick' with the javascript from the action? Maybe even only if the onclick evaluates to true. The current workaround is not very beautiful, does not work with autocomplete in most IDEs. I do not see any disadvantage in chaining. |
I think this project is abandoned. The real workaround is this:
Both |
@TheCoder4eu will you accept a pull request, which changes that behavior? If I find the time, I am willing to contribute this, but I want to be sure it will be merged. |
Hi @t-oster , |
@t-oster , |
The underlying issue is in AjaxRenderer.generateBootsFacesAJAXAndJavaScript
It first calls generateAJAXCallForASingleEvent here
ResponseRenderer.renderAttribute method with the original onclick attribute.Afterwards it generates the correct javascript and again calls the ResponseRenderer.renderAttribute method here
I could inject a Decorator for the ResponseRenderer class, which caches all renderAttribute calls and overwrites previous versions with later ones, but this does not make the code very readable. On the other hand I cannot see an easy way to swap the order of the calls, because the first call determines if the onclick handler already includes ajax. |
I have a better idea, will update the PR tomorrow: if A |
Hi @t-oster , |
The second version is probably easier to read. |
This xhtml:
<b:commandButton action="#{index.action}" value="Action" ajax="true" />
Generates the following (as expected):
<button type="submit" id="j_idt5:j_idt6" name="j_idt5:j_idt6" class="btn btn-default" onclick="BsF.ajax.callAjax(this, event,'j_idt5','j_idt5',null,null,null,null);;;return false;">Action</button>
But when setting the
onclick
attribute...<b:commandButton onclick="alert(1)" action="#{index.action}" value="Action" ajax="true" />
We get the following:
<button type="submit" id="j_idt5:j_idt6" name="j_idt5:j_idt6" class="btn btn-default" onclick="alert(1);">Action</button>
The
onclick
is overriden and the AJAX call is lost. The workaround is to set the ajax call by hand:<b:commandButton onclick="alert(1);ajax:index.action()" value="Action" ajax="true" />
This also affects other AJAX components, like
inputText
:<b:inputText value="hola" ajax="true" onchange="alert(1);" />
The text was updated successfully, but these errors were encountered: