Home Web Front-end JS Tutorial Introduction to AMD asynchronous module definition and how to use jQuery and jQuery plug-ins in Require.js_jquery

Introduction to AMD asynchronous module definition and how to use jQuery and jQuery plug-ins in Require.js_jquery

May 16, 2016 pm 04:45 PM
amd jquery

AMD Module

The overall goal of the AMD (Asynchronous Module Definition) format is to provide current developers with a usable modular JavaScript solution.

The AMD module format itself is a proposal for how to define modules so that both modules and dependencies can be loaded asynchronously. It has many unique advantages, including being inherently asynchronous and highly flexible, which can break the tight coupling between code and module identity that is common. It has been adopted by many projects, including jQuery (1.7).

RequireJS

RequireJS is a tool library mainly used for client module management. It allows the client code to be divided into modules for asynchronous or dynamic loading, thereby improving the performance and maintainability of the code. Its module management adheres to AMD specifications.

jQuery support for AMD

jQuery 1.7 starts to support registering jQuery as an AMD asynchronous module. There are many compatible script loaders (including RequireJS and curl) that can load modules using an asynchronous module format, which means it doesn't take much hacking to get everything working. You can take a look at the source code in jQuery 1.7:

Copy the code The code is as follows:

// Expose jQuery as an AMD module, but only for AMD loaders that
// understand the issues with loading multiple versions of jQuery
// in a page that all might call define(). The loader will indicate
/ / they have special allowances for multiple jQuery versions by
// specifying define.amd.jQuery = true. Register as a named module,
// since jQuery can be concatenated with other files that may use define,
// but not use a proper concatenation script that understands anonymous
// AMD modules. A named AMD is safest and most robust way to register.
// Lowercase jquery is used because AMD module names are derived from
// file names, and jQuery is normally delivered in a lowercase file name.
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
define( "jquery ", [], function () { return jQuery; } );
}

The way it works is that the script loader used indicates that it can support multiple jQuery versions by specifying a property, define.amd.jQuery, as true. If you are interested in knowing the specific implementation details, you can register jQuery as a named module, since there is a risk that it may be spliced ​​together with other files that use AMD's define() method without Use a suitable stitching script that understands anonymous AMD module definitions.

Higher version of jQuery (1.11.1) removes define.amd.jQuery judgment:

Copy code The code is as follows:

if ( typeof define === "function" && define .amd ) {
define( "jquery", [], function() {
return jQuery;
});
}

Use jQuery in Require.js

It is very convenient to use jQuery in Require.js, just configure it simply, for example:

Copy code The code is as follows:

// Simple configuration
require.config( {

// RequireJS loads all code through a relative path baseUrl. baseUrl is usually set to the same level directory of the script specified by the data-main attribute. baseUrl: "./js",

// Alias ​​of third-party script module, jquery is more concise and clear than libs/jquery-1.11.1.min.js;
paths: {

"jquery": "libs/jquery- 1.11.1.min.js"

}

});

// Start using jQuery module
require(["jquery"], function ($ ) {

//Your code
//You can use jquery methods directly here, such as: $( "#result" ).html( "Hello World!" );

});

Use jQuery plug-in in Require.js

Although jQuery supports AMD API, this does not mean that jQuery plug-in is also compatible with AMD.

General jQuery plug-in format:


Copy code The code is as follows:
(function ($) {
$.fn.myPlugin = function () {
//Your own plug-in code
};
})(jQuery);

However, we can use Require.js to load a jQuery plug-in with a slight modification:
Copy the code The code is as follows:

;(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD mode
define([ "jquery" ] , factory);
} else {
                                                                                                                                                                                                                              ) {
//Plug-in code
};
}));



Using jQuery UI components in Require.js

The use of jQuery UI components in Require.js is similar. Just modify the jQuery Widget Factory code and load the dependencies of jQuery UI. For example:


Copy code

The code is as follows:(function (widgetFactory) {
if (typeof define === "function" && define.amd) {
// AMD mode
define("jquery.ui.widget", ["jquery"], function () {

                                                                                                                       }
}
(function ($, undefined) {

// jQuery Widget Factory code

}));

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

AMD Radeon RX 7800M in OneXGPU 2 outperforms Nvidia RTX 4070 Laptop GPU AMD Radeon RX 7800M in OneXGPU 2 outperforms Nvidia RTX 4070 Laptop GPU Sep 09, 2024 am 06:35 AM

OneXGPU 2 is the first eGPUto feature the Radeon RX 7800M, a GPU that even AMD hasn't announced yet. As revealed by One-Netbook, the manufacturer of the external graphics card solution, the new AMD GPU is based on RDNA 3 architecture and has the Navi

ASUS unveils Adol Book 14 Air with AMD Ryzen 9 8945H and curious incense dispenser ASUS unveils Adol Book 14 Air with AMD Ryzen 9 8945H and curious incense dispenser Aug 01, 2024 am 11:12 AM

ASUS already offers various 14-inch laptops, including the Zenbook 14 OLED (curr. $1,079.99 on Amazon). Now, it has decided to introduce the Adol Book 14 Air, which looks like a typical 14-inch laptop on the face of it. However, an inconspicuous meta

Ryzen AI software gets support for new Strix Halo and Kraken Point AMD Ryzen processors Ryzen AI software gets support for new Strix Halo and Kraken Point AMD Ryzen processors Aug 01, 2024 am 06:39 AM

AMD Strix Point laptopshave just hit the market, and the next-gen Strix Halo processors are expected to be released sometime next year. However, the company has already added support for the Strix Halo and Krackan Point APUs to its Ryzen AI software.

AMD Z2 Extreme chip for handheld consoles tipped for an early 2025 launch AMD Z2 Extreme chip for handheld consoles tipped for an early 2025 launch Sep 07, 2024 am 06:38 AM

Even though AMD tailor-made the Ryzen Z1 Extreme (and its non-Extreme variant) for handheld consoles, the chip only ever found itself in two mainstream handhelds, the Asus ROG Ally (curr. $569 on Amazon) and Lenovo Legion Go (three if you count the R

AMD announces 'Sinkclose' high-severity vulnerability, affecting millions of Ryzen and EPYC processors AMD announces 'Sinkclose' high-severity vulnerability, affecting millions of Ryzen and EPYC processors Aug 10, 2024 pm 10:31 PM

According to news from this site on August 10, AMD officially confirmed that some EPYC and Ryzen processors have a new vulnerability called "Sinkclose" with the code "CVE-2023-31315", which may involve millions of AMD users around the world. So, what is Sinkclose? According to a report by WIRED, the vulnerability allows intruders to run malicious code in "System Management Mode (SMM)." Allegedly, intruders can use a type of malware called a bootkit to take control of the other party's system, and this malware cannot be detected by anti-virus software. Note from this site: System Management Mode (SMM) is a special CPU working mode designed to achieve advanced power management and operating system independent functions.

First Minisforum mini PC with Ryzen AI 9 HX 370 rumored to launch with high price tag First Minisforum mini PC with Ryzen AI 9 HX 370 rumored to launch with high price tag Sep 29, 2024 am 06:05 AM

Aoostar was among the first to announce a Strix Point mini PC, and later, Beelink launched the SER9with a soaring starting price tag of $999. Minisforum joined the party by teasingthe EliteMini AI370, and as the name suggests, it will be the company'

Beelink SER9: Compact AMD Zen 5 mini-PC announced with Radeon 890M iGPU but limited eGPU options Beelink SER9: Compact AMD Zen 5 mini-PC announced with Radeon 890M iGPU but limited eGPU options Sep 12, 2024 pm 12:16 PM

Beelink continues to introduce new mini-PCs and accompanying accessories at a rate of knots. To recap, little over a month has passed since it released the EQi12, EQR6 and the EX eGPU dock. Now, the company has turned its attention to AMD's new Strix

Deal | Lenovo ThinkPad P14s Gen 5 with 120Hz OLED, 64GB RAM and AMD Ryzen 7 Pro is 60% off right now Deal | Lenovo ThinkPad P14s Gen 5 with 120Hz OLED, 64GB RAM and AMD Ryzen 7 Pro is 60% off right now Sep 07, 2024 am 06:31 AM

Many students are going back to school these days, and some may notice that their old laptop isn't up to the task anymore. Some college students might even be in the market for a high-end business notebook with a gorgeous OLED screen, in which case t

See all articles