5 Replies Latest reply on Nov 3, 2012 12:58 PM by feder

    JQuery loading twice and overwriting my plugins

    devinderpal

      Hi,

      Using richfaces 4.2.2.

        I'm loading jQuery in my header and also loading some plugins such as jquery ui. I can use JQuery UI properly if there is no richfaces tag in my page. But once I use any richfaces tag like a4j:commandButton, it loads jquery again and wipes off all my previously loaded plugins. Hence I can't use any jquery ui methods anymore.

      I have tried using resource mapping to map both my jquery load and richfaces load to same Jquery URL (same version). Through firebug I can confirm that jquery from same URL is being loaded but still it's overwriting my plugin.

       

      Any solution to this? I have attached a sample application to explain the problem. Please help.

       

      Note: When I replace a4j:commandButton with h:commandButton on line 39 everything works fine.

        • 2. Re: JQuery loading twice and overwriting my plugins
          devinderpal

          I have already tried resource mapping. After resource mapping, it's using same version of JQuery  with same URL. But still overwriting my JQuery plugins.

           

          Any help will be appreciated

          • 3. Re: JQuery loading twice and overwriting my plugins
            yyq2009

            Hi, do it step by step following the article

            http://yyq2007aa.blog.163.com/blog/static/44181609201242794212780/

             

            and your resource mapping file should be:

             

            jquery.js=org.richfaces.staticResource/jquery.js

             

            and finally you should load jquery firstly yourself before your own javascript as the following line:

             

            <h:outputScript name="jquery.js" library="org.richfaces.staticResource"/>

             

            Path of jquery.js:

             

            YourWebApp/resources/org.richfaces.staticResource/jquery.js

             

            If you had read the article Re-routing JSF resource requests with RichFaces Resource Mapping, you would pay attention to one of the replies:

             

             

             

            Hey guys,


            this is really the issue I have introduced with limiting resources to specific libraries, thanks for pointing to that!


            I have reported it here: https://issues.jboss.org/browse/RF-11888


            Currently, as Grzegorz said, you need to move all the resources to some of those libraries, it means e.g.:


            # the configuration for mapping:
            jquery.js=org.richfaces.staticResource/jquery-alternative-version.js


            # where should the resource be located:
            {MAVEN_WAR_PROJECT}/src/main/webapp/resources/org.richfaces.staticResource
            {MAVEN_JAR_PROJECT}/src/main/resources/org.richfaces.staticResource/
            {JAR}/META-INF/resources/org.richfaces.staticResource/
            {WAR}/resources/org.richfaces.staticResource/

             

             

            Good luck.

             

               

             

             

            • 4. Re: JQuery loading twice and overwriting my plugins
              devinderpal

              This doesn't help. As I mentioned before, resource mapping can help in using same version of JQuery from the same URL. But still it will override JQuery plugins linked to first mention of jquery library.

               

              I think I have to raise a jira for this issue. I think richfaces should provide a configuration that can be used to restrict any reload of static resources.

              • 5. Re: JQuery loading twice and overwriting my plugins
                feder

                I agree with jboss fan99. We need to find a way to determine the sequence of the JS libraries in Richfaces 4.2.3+. The JIRA issue is recorded: https://issues.jboss.org/browse/RF-12537

                 

                Use Case: Implementing a JavaScript functionality, which is employing jquery.js library, is loaded before the RF-jQuery libraries. Subsequently the JavaScript DOM might be refreshed with objects by the 2nd initialisation of the jQuery library. The rebuild of these objects in jQuery (e.g. global variables/functions), remove the established objects of the primary loaded jQuery library.

                 

                Workaround: Place the script(s) of your custom functionality (which depends on the jQuery library, too), right below your html body tag. Remove the initialisation of <h:outputScript  name="js/jquery-1.7.1.min.js"> from your html header area. Ensure you are using at least one RF tag (e.g. <rich:inplaceInput .../>) in your xhtml file in order to trigger the RF javascript injection, which loads the jquery library once - and thus only once. Omiting a richface tag, makes your snippet throw an error.

                 

                Workaround Limitation: Note, if your snippet of JavaScript functionality demands jQuery functionality before the body html, of course, this workaround does little to support you.

                 

                (Awkward, I know. i.e. "workaround")

                 

                Solutions: If any solutions occur, please post to this thread.