Use Requirejs's shim parameter to complete the loading of the jquery plugin

Source: Internet
Author: User

Before the Requirejs framework, if we wanted to use the jquery framework, it would be loaded in the HTML page via the <script> tag, when the jquery framework generated global variables such as global variables $ and jquery. If the REQUIREJS framework is referenced in the project and the jquery is loaded in a modular way, jquery will no longer add global variables $ and jquery. Now that's the problem, although the jquery framework has started to support AMD specifications, many of jquery's plugins still don't support AMD and still need to use the global variable $ as before. jquery plugins are mostly structured as follows:

(function ($, undefined) {}) (JQuery);

If the jquery plugin is used in our project, but the jquery framework is loaded via REQUIREJS (no global variable is added), how do I complete the loading of the jquery plugin? Using a traditional party, loading the jquery plugin through <script> in an HTML page is definitely not going to work. This time we need to use

Requirejs the shim parameter to complete the loading of the jquery plugin. Let's take the slider plugin for loading jquery-ui as an example:

Requirejs.config ({shim: {'        jquery.ui.core ': [' jquery '],        ' jquery.ui.widget ': [' jquery '],        ' Jquery.ui.mouse ': [' jquery '],        ' jquery.ui.slider ': [' jquery ']    },paths: {jquery: ' Jquery-2.1.1/jquery ', Domready: ' Require-2.1.11/domready ', ' jquery.ui.core ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.core ', ' Jquery.ui.widget ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.widget ', ' jquery.ui.mouse ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.mouse ', ' jquery.ui.slider ': ' jquery-ui-1.10.4/ Development-bundle/ui/jquery.ui.slider '}); require ([' jquery ', ' domready ', ' jquery.ui.core ', ' jquery.ui.widget ', ' Jquery.ui.mouse ', ' Jquery.ui.slider '],function ($) {$ ("#slider"). Slider ({     value:0,     min:0,     max:4,     Step:1,     slide:function (event, UI) {}   );});

In the path parameter, we set the module name (optionally specified) and the JS file path mapping, and then in the shim parameter, specify the module name and its dependent array, above our jquery plug-in only rely on the jquery framework. In this way, you can use Requirejs to complete the loading of jquery and its plug-ins without the problem of global variable contamination.

Use Requirejs's shim parameter to complete the loading of the jquery plugin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.