5 Replies Latest reply on May 28, 2011 3:01 PM by ginc

    jsFunction: how to sync inputText value before actionListener gets invoked

    ginc Newbie

      Hi everyone,

       

      i have a the following elements:

       

       

      <h:inputText id="test" value="#{Bean.value}" />
      
      <a4j:jsFunction name="anyName" actionListener="#{Bean.doSomething}" render="test" />
      
      <a4j:commandButton id="testButton" value="test" actionListener="#{Bean.doSomething}" render="test" />
      

       

      The jsFunction is fired as soon as the user hits enter in the 'test' inputbox. (code for that left out for simplification purpose)

       

      The difference between the commandButton and the jsFunction is, that the inputText 'test' does not get syncronized with the Bean before the actionListener gets invoked, so therefore i cannot use the value in the actionlistener method.

       

      This behavior seems to have changed between richfaces 3.3.3 and richfaces 4.0.0.

      Back then, the behavior between the commandButton and the jsFunction was exactly the same.

       

      So, my question is, how can i make the behavior the same again? what do I need to change in order to have the sync done before the actionlistener of jsFunction gets invoked?

       

      Thanks

       

      Gregor

        • 1. Re: jsFunction: how to sync inputText value before actionListener gets invoked
          h2g2 Newbie

          Hi,

           

          there may be other cleaner solution, but this one should do it : instead of managing immediately the event, postpone it and repost it for a later phase (when you model will be updated).

          This should look like this with  :

          public final void aListener(final ActionEvent event) throws Exception {

            final PhaseId phaseId = event.getPhaseId();

            if (phaseId.equals(PhaseId.ANY_PHASE)) {

              // In order to retrieve data updated from the model before the managment of

              // the event, we postpone the management of the file upload event

              event.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);

              event.queue();

            } else if (phaseId.equals(PhaseId.UPDATE_MODEL_VALUES)) {

              // do all your stuffs here

              manageMyEvent(event);

            }

          }

          Hope this will help.

          • 2. Re: jsFunction: how to sync inputText value before actionListener gets invoked
            ginc Newbie

            Well, i guess this will work somehow, but as you said it is not a clean way of doing it.

             

            There has to be a way to submit the inputBox value to the backing bean, before the actionlistener invokes!

            Can somebody tell me how to achive this? (I still you like to work with jsFunction)

            • 3. Re: jsFunction: how to sync inputText value before actionListener gets invoked
              ginc Newbie

              ok, here is what i have done so far:

               

              First, I tried to achive it with a4j:param.

               

              <a4j:jsFunction name="anyName" actionListener="#{Bean.doSomething}" render="test">
                   <a4j:param name="param1" assignTo="#{Bean.inputValue}" />
              </a4j:jsFunction>
              

               

              if i call the function with "anyName('test')" the value 'test' does get assign to inputValue, but unfortunetely AFTER the actionListener gets invoked.

              This is not very useful, since i do need the value in the bean.

               

              So I came up with this (dirty) solution:

               

              Calling another jsFunction (tmpFunction('test')), which calls the actual jsFunction (anyName()).

               

              <a4j:jsFunction name="tmpFunction" oncomplete="anyName()">
                   <a4j:param name="param1" assignTo="#{Bean.inputValue}" />
              </a4j:jsFunction>
              
              <a4j:jsFunction name="anyName" actionListener="#{Bean.doSomething}" render="test" />
              

               

              This seems to be the only way to make sure the value is assigned before the backing bean is invoked.

              It works, but I don't like it.

               

              Does somebody know how solve this in a cleaner way??

               

              Thanks

               

              Gregor

              • 4. Re: jsFunction: how to sync inputText value before actionListener gets invoked
                Nick Belaevski Master

                For this code:

                 

                <a4j:jsFunction name="anyName" actionListener="#{Bean.doSomething}" render="test">
                     <a4j:param name="param1" assignTo="#{Bean.inputValue}" />
                </a4j:jsFunction>

                 

                Either action instead of actionListener or call doSomething via nested f:actionListener/a4j:actionListener tag. 

                • 5. Re: jsFunction: how to sync inputText value before actionListener gets invoked
                  ginc Newbie

                  That's it. This was the input I was looking for.

                   

                  Thank you very much Nick!

                   

                  Here is the working solution:

                   

                   

                  <a4j:jsFunction name="anyName" render="test">
                       <a4j:param name="param1" assignTo="#{Bean.inputValue}" />
                        <a4j:actionListener listener="#{Bean.doSomething}" />
                  </a4j:jsFunction>
                  

                   

                  calling it with: anyName('valueForInputValue');