1

I have this code:

<div id="visibleButtons">
    <p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
    </p:commandButton>
    <p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
    </p:commandButton>
    #{tutorSession.giveWarning}<!-- I will remove this line once the buttons are working -->
</div>

and what is happening is that the javascript function playNextHint is executing with the value of giveWarning from the last call, rather than the current one. I checked that the server is returning the right value; it is. In fact, I added that little bit of text output at the bottom to verify that the right value is being returned, and it always displays the way I expect, but the javascript still uses the old value.

Any thoughts on what might be happening?

The javascript method, in case it is relevant:

function playNextHint(giveWarning){
    alert(giveWarning);
}

Thank you in advance.

Edit: I found a work-around by using a hidden field with the value, and then getting the value of that field in the javascript, but that is messy and there has to be a better way, as I will be doing a lot of this sort of thing for this web app.

Edit 2: Sorry, I left in some code for demonstration purposes that seems to have confused people. First, I do NOT want to display giveWarning. That is just there so I could see that it was getting the right value. I left it in so people could see where the right value does show up, as opposed to where it does not.

Edit 3: What I want to do when the button is pressed is compute the new value of giveWarning on the server (that part works) and then send it to a javascript function to do logic based on it (the alert is a placeholder and for testing). The problem is that the value the javascript function gets always seems to be one click behind what it should be, even though when I just send the value tot he screen (the last line in the div) it displays what it should. Sorry for all the edits; like I said, it's my first time posting.

Melissa
  • 11
  • 3
  • in your `` What are you trying to update using `@parent` ? and your javascript function `playNextHint` doesn't make sense for me ? – Scorpion Feb 18 '15 at 11:16
  • I'm trying to update the content of #{tutorSession.giveWarning} in both buttons. Sorry about the method not making sense. I forgot to change it all the way back from my work-around version. The alert should be printing giveWarning. – Melissa Feb 18 '15 at 17:42
  • Ok, edited to be using the right line of code, now. Sorry about that. – Melissa Feb 18 '15 at 17:47

2 Answers2

0

I think you have just only taken the inappropriate approach. I have just make a new <outputLabel> and give it an id to use it in javascript method and added update="label" to update the label and eliminate passing any parameters to this js method. You can check this

In .xhtml

<h:form id="myForm"> 
      <p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="label" oncomplete="playNextHint();">
      </p:commandButton>
      <p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else"  update="label" oncomplete="playNextHint();">
      </p:commandButton>
      <p:commandButton id="skip" action="#{tutorSession.giveUp}" value="Skip">
      </p:commandButton>
      <h:outputLabel value="#{tutorSession.giveWarning}" id="label" />
</h:form>

and your js function

function playNextHint(){
     alert($('#myForm\\:label').text());
}

and in your ManagedBean tutorSession

    // other stuff here
    private String giveWarning;

    // setters / getters for giveWarning

    public void hintRequest() {
        setGiveWarning("hint");
    }

    public void newPathRequest() {
        setGiveWarning("newPath");
    }
Scorpion
  • 577
  • 4
  • 21
  • Thank you for responding so quickly. This seems similar to my work-around using the hiddenInput, though. Is there no way to just bass the boolean, which is what giveWarning is, to the javascript function? Eventually I'm going to take that alert out and do logic based on its value. – Melissa Feb 18 '15 at 17:45
  • Ok, I edited the question to elaborate on my problem. I think I wasn't very clear, the first time. – Melissa Feb 18 '15 at 17:54
  • No this is not applicable as i know, the only way is to make a hidden element and use its value in `js`. I don't know why you are seeing this as a messy solution ! – Scorpion Feb 19 '15 at 10:58
  • It's less that it's messy, as it is that I thought there was a neater way that I was messing up somehow. I saw on another stack overflow question (which I can no longer find or I'd link to it) someone say it could be done the way I originally tried, but now I'm guessing they are just mistaken. If the hidden element way is the best way to do it, then that's just how it is and I'll stop fretting over it. Thank you! Um... how do I accept a comment as an answer, though? – Melissa Feb 19 '15 at 21:41
0

I know this is an old question, but I encountered the same problem and fixed it using a separate remoteCommand:

<p:remoteCommand name="playNextHintComplete"
                 oncomplete="playNextHint(#{tutorSession.giveWarning})"/>

<p:commandButton value="Hint Please"
                 action="#{tutorSession.hintRequest}"
                 process="@form"
                 update="@form"
                 oncomplete="playNextHintComplete()"/>
CrypticDots
  • 314
  • 1
  • 8