[ 4 posts ]

Alberto Santini

YUI Contributor

  • Offline
  • Profile
Tags:

Conditional loading

Post Posted: Sat Feb 04, 2012 2:42 am
+0-
I moved a few conditional IE scripts from html to yuiConfig:

Code:
<!--[if lt IE 9]>
<script src="js/es5-shim.min.js"></script>
<![endif]-->
<!--[if lt IE 8]>
<script src="js/json2.js"></script>
<![endif]-->


The following result works fine, but I have an empty file (ie-fake.js):

Code:
       ie: {
            base: 'js/ie/',
            modules: {
                'ie-support': {
                    path: 'ie-fake.js'
                },
                'es5-shim': {
                    path: 'es5-shim.min.js',
                    condition: {
                        trigger: 'ie-support',
                        test: function (Y) {
                            return Y.UA.ie && Y.UA.ie < 9;
                        },
                        when: "instead"
                    }
                },
                'json2': {
                    path: 'json2.js',
                    condition: {
                        trigger: 'ie-support',
                        test: function (Y) {
                            return Y.UA.ie && Y.UA.ie < 8;
                        },
                        when: "instead"
                    }
                }
            }
        },
...
                'trammi': {
                    path: 'trammi.js',
                    requires: [
                        'ie-support',


I read the following post "Loader: Conditional module loading":
viewtopic.php?p=18485

I could not find a solution without a fake file.

Any suggestion?

Thanks,
Alberto

Jeff Craig

YUI Contributor

  • Username: foxxtrot
  • Joined: Thu Dec 04, 2008 9:20 am
  • Posts: 135
  • Location: Pullman, WA
  • Twitter: foxxtrot
  • GitHub: foxxtrot
  • Gists: foxxtrot
  • IRC: foxxtrot
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: Conditional loading

Post Posted: Sat Feb 04, 2012 11:58 am
+0-
What you're seeing here is why YUI offers wrappers around a lot of increasingly-native functionality (Y.JSON being lightweight wrappers around native JSON parsing, Y.Array offering lightweight wrappers for the functional programming constructs introduced in newer browsers). It provides consistent API without modifying native objects or adding new globals, both of which can break other scripts.

Now, while what you've done here does work, like you said, it forces you to have an extra empty file hanging around.

Personally, YUI provides the functionality provided in both es5-shim and json2 in other modules (json, json-parse, json-stringify for json2.js, various modules for es5-shim), so for a YUI application, I would never include these files, YUI's own module system will do better. If you're working in a mixed environment, which I suspect you might be and that's why you're doing this, this is probably the least-bad solution.

Steven Olmsted

YUI Contributor

  • Username: solmsted
  • Joined: Wed Apr 14, 2010 1:47 pm
  • Posts: 82
  • Location: Richmond, VA
  • GitHub: solmsted
  • Gists: solmsted
  • IRC: solmsted
  • Offline
  • Profile

Re: Conditional loading

Post Posted: Sat Feb 04, 2012 1:41 pm
+0-
As stated previously, you may not actually need these modules, but I think you could perform this conditional logic in your script instead of in the loader config. Just define the modules without the conditional stuff, then do something like this:

Code:
YUI(YUI_config).use('any', 'modules', 'you', 'need', function (Y) {
    var ieSupport;
    if (Y.UA.ie && Y.UA.ie < 9) {
        ieSupport = ['es5-shim'];
        if (Y.UA.ie < 8) {
            ieSupport.push('json2');
        }
        Y.use.apply(Y, ieSupport);
    }
});


Conditional loading is a really nice feature of loader, but in this case this feels a little bit cleaner than forcing all non-ie browsers to request an empty file.

Alberto Santini

YUI Contributor

  • Offline
  • Profile

Re: Conditional loading

Post Posted: Sun Feb 05, 2012 10:23 am
+0-
@Jeff, @Steven thanks for the replies.

As you said, I need those scripts for a thirdy parties library (the client side of a web socket impl - dnode).

The complete yuiConfig is the following one:
https://github.com/albertosantini/node- ... iConfig.js

I may not include those files and include a script, mapping es5/json2 capabilities with YUI features. I will review this detail later in the development. :)

Regards,
Alberto
  [ 4 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum