JSF execute Ajax before a onClick Event

爷,独闯天下 提交于 2019-12-11 06:24:46

问题


I have a Javascript function which changes the position of some elements according to some hiddenfields in the document, which are updated regularly with ajax.

Problem:

Since the javascript function (onclick) is executed before the Ajax (which reloads the element in which the hiddenfields are) the elements which should change position are always one turn behind.

What I want to achive:

The Problem would be solved if the ajax reload is executed before the onclick Event, so the reference to the hiddenfields would be correct. (and not on turn behind)

Is this possible in any way or is there another solution to this problem??

Code:

Call:

                <h:commandButton id="dice" onclick="animate()" alt="W&uuml;rfel mit einer Eins" image="resources/img/wuerfel1.png" action="#{spiel.dice()}" tabindex="4" title="W&uuml;rfel mit einer Eins">
                    <f:ajax render="gameinfo" />                            
                </h:commandButton>

Javascript Function:

    function animate() {
        var newPlayer1 = document.getElementById('player1score').value;
        var newPlayer2 = document.getElementById('player2score').value;
        // Spieler 1 Animation
        $("#player1").fadeOut(700, function() {
            $("#player1").appendTo(newPlayer1);
            $("#player1").fadeIn(700);
});
        // Spieler2 Animation
        $("#player2").delay(1400).fadeOut(700, function() {
            $("#player2").appendTo(newPlayer2);
            $("#player2").fadeIn(700);
});
    }

回答1:


You should use the onevent of the f:ajax

  <h:commandButton id="dice" alt="W&uuml;rfel mit einer Eins" image="resources/img/wuerfel1.png" action="#{spiel.dice()}" tabindex="4" title="W&uuml;rfel mit einer Eins">
      <f:ajax render="gameinfo" onevent="animate" />                            
  </h:commandButton>

change your animate function into

function animate(data) {
    if (data.status === 'success') {
        //your original code goes here...
    }
}

Also : read this answer by BalusC : Proccess onclick function after ajax call



来源:https://stackoverflow.com/questions/16443866/jsf-execute-ajax-before-a-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!