Load jQuery and plugins in YUI

夙愿已清 提交于 2019-12-06 15:19:10

You're on the right track with using onProgress, but you don't need to jump through all the hoops of cloning and faking module.exports. The trick is to use jQuery.noConflict().

jQuery.noConflict does two things:

  • It returns the jQuery function
  • It restores a previous version of jQuery if it was present, or it removes jQuery from the global object

Here's an illustration of how it works:

<script src="jquery.1.8.js"></script>
<script>
var jQuery18 = jQuery;
</script>
<script src="jquery.1.9.js"></script>
<script>
// the true parameter makes this work for both the $ and jQuery global variables, not just the $
var jQuery19 = jQuery.noConflict(true);
alert('is the global jQuery is jQuery19? ' + (jQuery === jQuery19)); // false
alert('is jQuery19 the same as jQuery18? ' + (jQuery19 === jQuery19)); // false
alert('is the global jQuery the same as jQuery18? ' + (jQuery === jQuery18)); // true
</script>

Another issue is that since YUI expects scripts with concatenated modules, e.name doesn't refer to the name of the module, but instead to the URL of the script that was just loaded. The names of the modules loaded in that script can be found in an array in e.data. This just means that you need to iterate over e.data to figure out if jQuery was loaded.

To sum it up, your script should look like this:

YUI({
  modules: {
    'jquery': {
      fullpath: '//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js'
    }
  },
  onProgress: function (e) {
    for (var _module, i = 0, length = e.data.length; i < length; i++) {
      _module = e.data[i];
      if (_module.name === 'jquery') {
        // trap jQuery here while removing it from the global object
        (function ($) {
          YUI.add('jquery', function (Y) {
            Y.jQuery = $;
          });
        }(jQuery.noConflict(true)));
      }
    }
  }
}).use('jquery', function (Y) {
  var $ = Y.jQuery;
  $('foo').attr('bar', 'baz');
});

Plugins could be an issue if the scripts that contain them assume jQuery is in the global object (most do). So I suggest you host them yourself and wrap them in YUI.add.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!