SEAJS Modular Loading Framework uses

Source: Internet
Author: User

Seajs is a modular loading framework that follows the CMD specification

COMMONJS,CMD,AMD and other specifications will be mentioned here, the main first to understand how to use in the code.

If you've ever used Nodejs, it's much easier to understand.

We use Sea.js to load our defined modules (which now follow the CMD specification) and work with the corresponding data.

First of all, of course, to download sea.js, you can directly go to http://seajs.org/docs/#downloads directly download the code package, extracted in the/dist/directory can be found Sea.js

The CMD specification is lazy loading, loading on demand, that is, the corresponding module will be loaded in require.

The basic usage is:

Define (function(require, exports, module) {  // The module code goes here
Require.
});

CMD detailed usage See this

For example: (For the sake of a brief explanation, put it directly in the same directory)

Index.html is the main interface, Main.js here acts as the main module file (generally need to seajs.use ('. Main ') to load the main module), and then the main module main also calls the Main1,main2 small module, understanding the execution process.

Index.html:

The first includes the resource Sea.js, and then the main module, where the callback function is used because the data returned in the main module is executed.

<!DOCTYPE HTML><HTML><Head>    <title>Seajs</title>    <styletype= "Text/css">    </style></Head><Body><Scripttype= "Text/javascript"src= "./sea.js"></Script><Scripttype= "Text/javascript">//Load Portal file main.js, default suffix JS auto matchSeajs.use ('./main',function(Main) {Console.log (Main.say ()); });</Script></Body></HTML>

Main.js:

Here, Main.js defines a module main.js, where it require other modules for processing, and then returns an object.

Return can be used to directly use return, the type will be automatically judged, or can be module.exports =

For example, to return ' W ', You can return ' W ' directly; or module.exports = ' W '; Index there will be a corresponding modification on the line.

Define (function(require,exports,module) {     console.log (' module of Main: ');     var main1 = require (' main1 ');    Main1.say ();     var main2 = require (' main2 ');    Main2.say ();     return {         function() {             console.log (' Main--hello ');        }    };});

Main1.js:

Define (function(require,exports,module) {     console.log (' module of main1: ');     = {         function() {             console.log (' Main1--hello ');        }    };});

Main2.js:

Define (function(require,exports,module) {     console.log (' module of main2: ');     return {         function() {             console.log (' Main2--hello ');        }    };});

OK browser to access index.html to see the results of the execution:

SEAJS Modular Loading Framework 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.