Require.js Getting Started notes

Source: Internet
Author: User

Web site more and more large, JS is more write more. When all JS is concentrated in the head part of HTML, the page load becomes very slow, many of the JS code is not all applicable to the current page, resulting in a very high degree of redundancy of the code.

And the long JS code, do not have a good modular, for future maintenance caused great difficulties.

For this purpose to understand the use of the next require.js and usage, for later code refactoring and modularity to help. Let the latter be better able to manage the code.

1 Why should I use Require.js?

Recently in the production of a website. introduced

<script src= "Static/script/jquery.js" ></script><script src= "Static/script/jquery.lazyload.min.js" ></script><script src= "Static/script/swiper.js" ></script><script src= "static/script/ Laydate/laydate.js "></script>    ...    <script scr= "Static/script/common.js" ></script><!--business code--

The HTML in turn introduced the JS code, in the loading of these JS, the page will stop rendering, until the JS file loading is complete.

There is a dependency between the different JS, it is necessary to ensure that the JS files are executed sequentially, and when the dependencies become complex, writing and maintaining will become extremely difficult.

For this reason we introduced require.js, precisely to solve these two problems.

2 loading require in the page

Go to the official website to download the newest require.js first.

Reference the JS in the HTML header

  

<script data-main= "Static/script/main" src= "Static/script/require.js" ></script>

Data-main is a required property that specifies the Require.js portal file. The browser will automatically load the portal file when the Require.js is finished loading

3 Loading JavaScript

Use Require.config to manage our JS

Require.config ({baseUrl: "Static/script", Paths:{jquery: ' jquery ', lazy: ' jquery.lazyload ' common: ' Common ', Swiper: ' Swiper ', laydate: ' Laydate/laydate ',                //testing test                : ' Test ',}});

Using Require.config, use BaseURL and "Paths" config to set the module ID.

4 Defining modules

Defining modules in Test.js

A JS file should only define a module, the module Yunxi loading multiple modules, the order is uncertain, but the order of dependence will eventually be correct.

define ([' jquery '],function ($) {var width = 1000;return {//some function () setwidth (): {width = $ (window). Width ()}, GetWidth (): {return width;}};});

  

5 Load module.

  

Require ([' Test '],function (test) {    test.setwidth ();  Output Width    alert (test.getwidth ());});

6 loading non-canonical modules

When the module is not defined by the canonical define (), we need to shim

Require.config ({paths:{jquery: ' jquery-2.1.4.min ',},shim:{' test ': {//the non-canonical module requires a dependency deps:[' jquery '],exports: ' Test ' ,}},});

  

Require.js Getting Started notes

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.