We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24935
    • 160 Posts
    There are many times when it is necessary to do extra work to avoid the same script from being included twice. For example, if you have two polls, the first snippet call should register the necessary js library, the second should not. As it is, it is necessary to add a parameter such as &disable_js_libs to many snippets to handle this. It is a small inconvenience, but those ’gotchas’ add unnecessary complexity for beginning users.

    Another example is a page that uses MooTools works fine, until you log into the manager and QuickEdit includes MooTools again and breaks the page.

    It would be nice if these conflicts were handled automatically. I suggest adding two or three optional arguments to regClientScript and regClientStartupScript. They would look something like this:
    string regClientScript($src,$identifier='',$version='',$mode=HIGHEST_VERSION)
    


    So if one script called
    regClientScript('assets/someplace/jquery.js','jQuery','1.3.1');
    

    and another script called
    regClientScript('assets/someotherplace/jquery.js','jQuery','1.3.2');
    

    only the second script would be registered, since it has a higher version number.

    For snippets that can be included multiple times in the same page, just using
    regClientStartupScript('assets/snippets/mysnipname/mysnipname.js','mysnipname');
    

    Would only register the script the first time, not multiple times. That would be very nice.

    One more feature:
    regClientScript('assets/someplace/lib.js','EXTJS','2.2.1');
    regClientStatupScript('assets/someotherplace/lib.js','EXTJS','2.1');
    

    would register the 2.2.1 version as a STARTUP script (if any of the calls are for a startup script, the selected version will be put in the head of the document).

    What do you think of this idea? I don’t think it would be very hard to write the code for it.
      • 25663 MODX Staff
      • 12,272 Posts
      That sounds like a good addition. Care to take a stab at some code and post it over in JIRA?
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 22303 MODX Staff
        • 10,725 Posts
        I actually have an idea on how to implement this without adding a bunch of specific parameters and without breaking compatibility with existing calls, and I’d like to have this change made in parallel in both Evo and Revo. Basically, we will convert the existing plaintext parameter to an options parameter that accepts an array of named options matching the new parameters suggested by rfoster, and if a boolean value is encountered, it will simply convert that to an array with a plaintext element. This will also preserve the plaintext functionality and allow additional features down the road without having to modify the method signature at all.
          • 24935
          • 160 Posts
          Sure. I’ll make it according to those specifications.
            • 24935
            • 160 Posts
            Code for Evolution attached to MODX-898

            It seems to work fine, but someone should look at it to see if it can be optimized for better speed.
              • 25663 MODX Staff
              • 12,272 Posts
              Thank you Mr. Foster. smiley
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                • 24935
                • 160 Posts
                I actually think my code is a bit of an overkill, as it handles some cases that will rarely happen. Speed is probably more important than handling rare situations.
                  • 17499 ☆ A M B ☆
                  • 872 Posts
                  Anything new about this subject ? Is it coming soon ? Like in Revolution 2.1 ? smiley
                    • 24935
                    • 160 Posts
                    Revo implements has a minimal implementation that is less complicated than I originally proposed. It just keeps track of any js/css that is added via regClientScript, regClientStartupScript, and regClientCSS and doesn’t bother the add the script if it is an exact match of something already added.

                    In some cases, this could be a problem--for example if one snippet adds a js lib in the footer and later another snippet adds it to the head, it will only end up in the footer IF the urls to the js lib are identical. In most cases the urls will be different, so it will work as it always has, and include both.
                      • 17499 ☆ A M B ☆
                      • 872 Posts
                      It doesn’t really solve the matter then.

                      I really liked the proposition above on which i would add one additional parameter to handle dependancies.
                      That would be welcome for third party components, or even manager UI where one could be able to load a selection of ext components instead of the whole MODExt package...