Seajs 使用总结

来源:互联网 发布:md5加密 java代码 编辑:程序博客网 时间:2024/06/08 11:58

1、定义一个js

     define(dunction(require,exports,module){

});

require:主要功能是访问其他模块提供的API,调用该模块的方法,

For example:defune(function(require){

      var test=require('./test');

      test.do();// 调用了test中的do方法

});

exports: 主要功能是提供模块的API,定义一个对外的接口。

define(function(require,exports,module){

     function Darw(){

     this.id=1;

     this.width=10;

     this.text="深度";

}

module.exports=Darw;

});

return 也可以实现这一样的功能

define(function(require,exports,module){

    return {doSomething:function(){

     }};

});

module:存储模块的元信息,包含如下信息:

module.id 当前模块的唯一标示

module.Dependencies 依赖列表

module.exports 可以对export参数进行自定义{没明白?}

module.constructor 为所有的module参数对象添加一些公用属性或方法。


seajs.use('./example',function(example){

     example.sayHello();

});

第一个参数就是指定的一个模块,

第二个参数是一个函数函数的参数就是一个模块的实例。


2、引入第三方框架jQuery

define(function(){

/*        jQuery 源码              */

return $.noConflict();

});

//应用方法

var $=require("jquery");

$("#id").click(.....);

3、异步的加载多个模块

requrie.async(['./c','./d'],function(c,d){

c.do();

d.do();

});

4、别名配置

添加一个js文件config.js

内容可以为

seajs.config({
    // Enable plugins
    plugins : [ 'shim' ],

    // Configure alias
    alias : {
        'jquery' : {
            src : 'lib/jquery-1.8.2.min.js',
            exports : 'jQuery'
            
        },

        'jquery.contextMenu' : {
            src : 'lib/jQcontextMenu/jquery.contextMenu.js',
            deps : [ 'jquery' ]
        },

});

在引入模块的时候就可以使用别名了

5、在页面用引入

 <script src="webDraw/js/seajs/sea.js"
        id="seajsnode"
        data-config="config.js"
        data-main="init.js"></script>






0 0