Using Mini-define to implement modular management of front-end code _JAVASCRIPT skills

Source: Internet
Author: User
Tags define function

Mini-define

A simple front-end modular framework based on require implementation. If you don't want to spend time learning require.js, and don't want to look at long CMD/AMD specifications, then this mini-define is a good choice for you. If you've used Sea.js or require.js before, then mini-define is more efficient, lighter, and easier to use. Project Address: GitHub

Usage

First define the module

Defining modules

One: Define the module with define function

1.1 Depending on whether there are dependencies, there are two situations:

1.1.1: No dependent modules

Copy Code code as follows:

Define (' id ', function () {
Put your code here
});

1.1.2: A dependent module

Copy Code code as follows:

Define (' id ', [' Modea ', ' Modeb '],function (a,b) {
Put your code here
});

1.2 Depending on the need to return processing results to external use, but also can be divided into two cases:

1.2.1 Have returned objects:

Copy Code code as follows:

Define (' id ', function () {
return {
Put your code here
}
});

1.2.2 does not return an object

Copy Code code as follows:

Define (' id ', function () {
Put your code here
});

Second: Calling the module with the Require () function

2.1 Depending on the number of modules requested, there are two possible scenarios:

2.1.1. Call a single module

Require (' Modeid ')

2.1.2. Calling multiple modules
Require ([' Modea ', ' modeb ']);
2.2 According to whether there is callback processing, but also can be divided into two cases:

2.2.1 has callback handler function

Copy Code code as follows:

Require (' Modeid ', function (mode) {
Put your code here
});

Require ([' Modea ', ' Modeb '],function (a,b) {
Put your code here
});

2.2.2 No callback processing
Require (' Modeid ');
Then in the Index.html page, refer to the desired module in turn

Copy Code code as follows:

<!--core module-->
<script src= "Lib/core/require.js" ></script>
<!--module--> for demonstration
<script src= "Lib/main.js" ></script>
<script src= "Lib/config.js" ></script>
<script src= "Lib/init.js" ></script>

The last is to use the way you like the Lib directory to merge compression, generate a min.js file. In the release of the application, the corresponding index.html also need to adjust:

Copy Code code as follows:

<script src= "Lib/min.js" ></script>

Advantages:

In contrast to Seajs.js or original require.js, the addition of comments to Hiberai lines of code used lightweight to describe all fat, is completely bone.
There is no advanced content, there is no complex skills, almost zero learning costs.

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.