Seajs 2.3.0 getting started, seajs2.3.0
[Seajs]
A long time ago, I tried seajs many times. It may be because I am stupid or the documents and examples of seaj are too simple to start,
This time I made a website myself and reduced the http request when referencing multiple js files,
After searching for half a day, I found seajs + spm again. Okay, let's try again.
[Blog]
This blog post is very thorough and seems to have been written by seajs developers for reference. If this blog post can be understood, you don't have to read it any more,
Address: https://speakerdeck.com/lifesinger/seajs
[Getting started with dummies]
If you still can't understand it, let's take a look at it step by step.
Note:
My example is written using freemarker. If you don't know this, you need to rewrite it to html,
In fact, there is no difference, that is, the path needs to be changed. If you know, you can directly run tomcat.
[Demo01]
Demo01.ftl
<! DOCTYPE html>
Demo01.js
// A js file in seajs is a module. The module definition defines define (function () {alert ('20140901 ');});
Running effect:
[Demo02]
Demo02.ftl
<! DOCTYPE html>
Demo02.js
define(function(require, exports){exports.msg = '222';});
Running effect:
[Demo03]
Demo03.ftl
<! DOCTYPE html>
Demo03.js
define(function(require, exports){var demo = require('./demo.js');var str = demo.getStr('test');exports.msg = 'the res is:' + str;});
Demo. js
define(function(require, exports){exports.getStr = function(ss){if(ss == '1'){return '111';}else{return '222';}};});
Running effect:
[Simple description]
Take demo03 for example:
1. Each js is a module and the definition must use define.
2. Each module has two parameters: require and exports.
3. require defines the module that this module depends on.
4. exports defines the external methods provided by this module to other modules. Other modules can call these methods after obtaining this module.
5. use the seajs. use module when using it.