This article mainly introduces how to use mini-define to implement modular management of front-end code. it is a very good article. we recommend it to partners who need it.
Mini-define
A simple front-end modular framework based on require. If you don't want to spend time learning require. js or look at the long cmd/amd specifications, then this mini-define is your good choice. If you have used sea. js or require. js before, mini-define is more efficient, lightweight, and easy to use. Project address: github
Usage
First define the module
Definition module
I. use the define function to define a module
1.1 depending on whether there is dependency, there are two cases:
1.1.1: No dependent module
The code is as follows:
Define ('id', function (){
// Put your code here
});
1.1.2: dependent modules
The code is as follows:
Define ('id', ['modea ', 'modeb'], function (A, B ){
// Put your code here
});
1.2 if you want to return the processing result for external use, there are two situations:
1.2.1 returned objects:
The code is as follows:
Define ('id', function (){
Return {
// Put your code here
}
});
1.2.2 no objects returned
The code is as follows:
Define ('id', function (){
// Put your code here
});
II. use the require () function to call the module.
2.1 depending on the number of requested modules, there are two possible cases:
2.1.1. call a single module
Require ('modeid ')
2.1.2. call multiple modules
Require (['modea ', 'modeb']);
2.2 based on whether callback processing exists, there are two possible scenarios:
2.2.1 callback handler
The code is 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 ');
Reference the required modules on the index.html page.
The code is as follows: