SEAJS implementation of JavaScript module development and loading by module

Source: Internet
Author: User
Tags define function script tag

SEAJS implements the module development of JavaScript and loads it by module. Used to solve the tedious JS naming conflicts, file dependencies and other issues, the main purpose is to make JavaScript development modular and easy to Load.

Official Document: Http://seajs.org/docs/#docs

First look at how SEAJS is developing the Module. Using Seajs basically there is only one function "define"

Fn.define = function (id, deps, factory) {    //code of  function ...}

Use the Define function to define a module that defines the specification according to the CMD (Common module Definition) module. A file is a module. So there is only one define in a JS file, that is, a file is a module, the concept of a module in Seajs is somewhat similar to the object-oriented Class--modules can have data and methods, data and methods can be defined as public or private, and public data and methods can be called by other Modules.

In the official instance, define only passes one Parameter: one factory function

Define (function (require, exports, Module) {    //code of the  module ...});

Before we look at the parameters, let's look at how define handles the Parameters:

Fn.define = function (id, deps, Factory)

The parameters that can be received by define are module id, module array and factory function, respectively;

If there is only one argument, assign the value to Factory.

If there are two parameters, the second is assigned to factory, and the first one is assigned to Deps if it is an array, otherwise it is assigned to the ID.

If there are three parameters, the values are assigned to Id,deps and factory respectively.

According to the official example, the proposal or according to the official to pass a parameter, that is, only the factory parameter, so that the module ID default to the JS file path.

Take a look at the parameters of the factory function:

require,  exports, Module

require-module load function for documenting dependent modules. This parameter is for loading the other Modules.

exports-the interface point at which the data or method is defined to expose it to an external call. This parameter is used to expose the method of this module

module-the Meta-data of the Modules. It is an object that has Properties:

module.id--the ID of the Module.

module.dependencies-an array that stores the list of IDs of all modules that this module relies On.

module.exports--points to the same object as Exports.

Use of parameters using Factory:

Define (function (require, exports, Module) {    var a  = Require ('./a ');//introduce A module           exports.fun2= function () {            Alert ("fun2");            A.dosomething (); }    }

The Require object is used to load the module, but the regular expression is used to validate the load, so its argument must be a specific string and cannot be an Expression.

Seajs will automatically add ". js" to the require parameter to load, i.e. no suffix is required, but in some cases seajs will not add ". js" to It:

1. Load Css:

Require ("./module1-style.css");

2. The path contains the "? ”:

Require (<a  href= "http://example/js/a.json?b=func" >http://example/js/a.json?cb=func</a>);

3. The path ends with "#":

Require ("http://example/js/a.json#");

Use exports to expose methods, or properties, to the Outside. There are several ways to expose attributes, such as the one in the above example, and the Other:

Use Return:

Define (function (require) {    var data1 = 1;//private data    var func1  = function () {//private method        Returna.run (data1);    }    //exposed    return{        data2:2,        func2:function () {            return ' Hello ';        }    ;});

Return the JSON object directly:

Define ({    data:1,    func:function () {        return ' Hello ';    }});

page, Call Seajs

The seajs used to solve the module development, and cumbersome file dependencies, so all the file dependencies are loaded through the Require object, or you can use Require.async ("./a") to load asynchronously, that is, when the use of the time to load,

Note: either require () or Require.async () can have a second parameter called the callback function, which is invoked when the file is loaded successfully:

require.async(‘/path/to/module/file‘,function(m) {

     //code of callback... });With this load-dependent approach, we don't have to write a lot of <script ></script> tags on the page, just using a
<script src= "./sea.js" data-main= "./init" ></script>

, where data-main= "./init" is the ingress module, which means that the module is loaded, and the module that it depends on is iterated, using this property can omit Seajs.use. This one says Later.

Use Seajs.config for global configuration:

Seajs.config ({    base: ' path/to/jslib/',    alias: {      ' app ': ' path/to/app/'    },    charset: ' Utf-8 ',    timeout:20000,    debug:false});

This is a global configuration of the seajs,

base Base Address path when base address is addressed

alias can set abbreviations for longer common paths.

CharSet represents the CharSet property of the script tag when downloading js.

Timeout indicates the maximum length of time, in milliseconds, to download a file.

Debug Indicates whether it is working in debug mode.

Use Seajs.use to load the ingress module, which is equivalent to the main function of java,

Single module mode seajs.use ('./a '); Callback mode Seajs.use ('./a ', function (a) {  a.dosomething ();});//multi-module mode seajs.use (['./a ', './b  '],function A.dosomething ();  B.dosomething ();});

The following can be seen in its official simple example of the page how to write, basically understand how sea.js is used:

From: http://www.cnblogs.com/lic309/p/4136389.html

<script src= ". /sea-modules/seajs/seajs/2.2.0/sea.js "></script><script>  //Set Configuration  Seajs.config ({    base: "). /sea-modules/",    alias: {      " jquery ":" jquery/jquery/1.10.1/jquery.js "    }  });  For development  if (location.href.indexOf ("? dev") > 0) {    seajs.use (". /static/hello/src/main ");  }  For production  else {    seajs.use ("examples/hello/1.0.0/main");  } </script>

Mian.js

Hello/mian.jsdefine (function (require) {  var Spinning = require ('./spinning ');  var s = new Spinning (' #container ');  S.render ();});

SEAJS implementation of JavaScript module development and loading by module

Related Article

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.