Requirejs Simple introduction that uses

Source: Internet
Author: User

Requirejs Introduction

Requirejs is a JavaScript module loader. It is ideal for use in browsers. Loading a modular script with Requirejs will increase the load speed and quality of your code.

compatibility

Browser (browser)

is compatible
IE 6+ Compatible?
Firefox Compatible?
Safari 3.2+ Compatible?
Chrome Compatible?
Opera Compatible?
Advantages
Realize the asynchronous loading of JS file, avoid the dependency between the Web page loss Response Management module, and facilitate the writing and maintenance of the code.
Get started quickly
    • Step 1
      • Introduction of Require.js
      • The dependency in require () is an array, even if there is only one dependency, you must also use an array to define
      • The second parameter is a callback function (callback) that can be used to resolve dependencies between modules
<! DOCTYPE html>
    • Step 2
      • Require.config is used to configure the module load location
      • If the fixed position is longer, you can use BASEURL: "JS", then the paths will not have to write JS
<! DOCTYPE html>
    • Step 3
      • The Require.config configuration is repeated in step 2, which is not good if each page is added to the configuration, and Requirejs provides a function called "Master data".
      • Create a main.js put the Step 2 require.config in the Main.js
<script data-main= "Js/main" src= "Js/require.js" ></script>
    • Step 4
      • Modules loaded through require are generally required to comply with the AMD specification, using define to declare the module, but in some cases need to load non-AMD specification JS, this time need to use another function: Shim
Require.config ({    shim: {        "underscore": {            exports: "_";        },        "Jquery.form": ["jquery"]    }}); Require (["jquery", "jquery.form"], function ($) {    $ (function () {        $ ("#form"). Ajaxsubmit ({...})    ;});
links

Nanyi about JS Modular programming Instructions:

    • Http://www.ruanyifeng.com/blog/2012/10/javascript_module.html
    • Http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html
    • Http://www.ruanyifeng.com/blog/2012/11/require_js.html

Requirejs Simple introduction that uses

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.