8 Replies Latest reply on Aug 14, 2012 10:39 AM by healeyb

    jsFunction data executing backing bean function twice

    garagoth

      m I misusing Hello everyone,

       

      I am having a strange problem with a4j:jsFUnction.

      My setup is as following:

      GlassFish Server Open Source Edition 3.1.2.2 (build 5)

      RichFaces 4.2.2.Final (tried with 4.3.0 M1 as well)

       

      I have a simple jsf2 page with <a4j:jsFunction name="fetchDataFromBean" data="#{sessionBean.fetchData()}" oncomplete="do_some_stuff_with_data(event.data);"/>

       

      fetchData() is simple:

      public Object[] fetchData() { /* process... and return Object[] */ }

       

      Then every time I call myFunction() from javascript (or any onsomething event), there is ONE request to serwer (checked that with firebug), method fetchData() in my backing bean gets called TWICE (I am logging this in server log), second one immediately after first one finishes executing, and results of SECOND method execution are returned back to browser (to event.data).

       

      Am I misusing jsFunction or is this a bug in RF?

      How can I make it work as expected?

       

      Regards,

      Marcin.

        • 1. Re: jsFunction data executing backing bean function twice
          healeyb

          I don't think it's intended that the data attribute value should be a method call, the only way I've ever used this is where

          it references a String in a backing bean, and you know what JSF is like with calling getters loads of times.

           

          I'd suggest using an action routine (not actionListener, unless they've fixed the bug where the actionListener executes

          before the a4j:param setter) to setup the data which will then be retrieved by the data attribute getter. I'm not sure

          what data types are supported but you could experiment.

           

          Regards,

          Brendan.

          • 2. Re: jsFunction data executing backing bean function twice
            snjv180

            Yes I have a same kind of problem and have used  a flag to check if data has been already loaded and Brendan is absolutely correct about JSF calling the getters. Mine queried the database nearly 4 to 5 times before displaying the result. I would very much appreciate a concrete and stable solution if there is one.

            • 3. Re: jsFunction data executing backing bean function twice
              healeyb

              The golden rule is simply never access the database from a getter, or you'll have serious performance problems.

              • 4. Re: jsFunction data executing backing bean function twice
                snjv180

                So how should this be implemented. Are you suggesting binding or any other way through which this would be possible. I mean if I need data and there are just so many records and I need to extract a subset of these record what should I do to resolve this problem.

                • 5. Re: jsFunction data executing backing bean function twice
                  garagoth

                  Hi,

                   

                  With calling action and data as property, it still gets called twice (getter for data that is).

                  Even stranger, it calls: getter for data, action method, getter for data (again)

                   

                  Regards,

                  Marcin.

                  • 6. Re: jsFunction data executing backing bean function twice
                    healeyb

                    I never need to use binding, in a backing bean have a List<Something> mylist and <rich:dataTable value="#{bean.mylist}" ...>. So

                    then you need to populate this list somehow. Often I will use a f:event preRenderView event listener to load the data when the page

                    loads.

                     

                    Other times the dataTable may be in a tabPanel (not the first tab) so I'll load mylist in a tab change listener. Or maybe the user has

                    to make some selections before the data can be dislayed in the table in which case the data may be loaded in a value change

                    listener or an action method.

                     

                    It's really just a case of leaving the loading of the data to the last possible moment. Care needs to be taken to ensure that the list

                    is reloaded after any changes are made to the underlying data (obvious but I often forget!).

                     

                    Regards,

                    Brendan.

                    1 of 1 people found this helpful
                    • 7. Re: jsFunction data executing backing bean function twice
                      snjv180

                      Thank you Brendan,

                       

                      Yes I see now. Thank you for a wonderful answer and sharing your idea. I hope I can implement it the same way if possible and make my application faster.

                      • 8. Re: jsFunction data executing backing bean function twice
                        healeyb

                        If you output this in the getter using a logger or System.out.println it will show which phase it's being called

                        in which may or may not shed some light in what's going on (probably not in fact):

                         

                        FacesContext.getCurrentInstance().getCurrentPhaseId().toString()