发布于 2015-05-21 02:57:28 | 174 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

SeaJS JavaScript模块加载框架

SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制。与jQuery等JavaScript框架不同,SeaJS不会扩展封装语言特性,而只是实现JavaScript的模块化及按模块加载。SeaJS的主要目的是令JavaScript开发模块化并可以轻松愉悦进行加载,将前端工程师从繁重的JavaScript文件及对象依赖处理中解放出来,可以专注于代码本身的逻辑。


http://localhost/seajs/index.html

<!doctype html>
<head>
<title>Hello Seajs</title>
    <script src="sea.js"></script>
    <script>
        seajs.config({
          // 调试模式
          debug: true,
          // Sea.js 的基础路径
          base: ‘http://localhost/seajs/app‘,
          // 别名配置
          alias: {
                "jquery": "jquery/jquery"
          },
          // 文件编码
          charset: ‘utf-8‘
        });
    </script>
    <script>
            //执行模块
            seajs.use("app.js"); 
    </script>

</head>
<body>
    <div id="hello-seajs" >Hello SeaJS</div>
</body>
</html>

http://localhost/seajs/app/jquery/jquery.js

http://localhost/seajs/app/app.js

define(function(require,exports,module){
    var $ = require("jquery");
     // 引入util模块的接口
     var util = require(‘./util‘);
     //var helloSeaJS = document.getElementById(‘hello-seajs‘);
     // 调用接口的方法
     //helloSeaJS.style.color = util.randomColor();
     var helloSeaJS = $("#hello-seajs");
     helloSeaJS.css("color",util.randomColor());
});

http://localhost/seajs/app/util.js

define(function(require,exports,module){
     var util = {};    
     var colorRange = [‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘];
     util.randomColor = function(){
          return ‘#‘ +
               colorRange[Math.floor(Math.random() * 16)] +
               colorRange[Math.floor(Math.random() * 16)] +
               colorRange[Math.floor(Math.random() * 16)] +
               colorRange[Math.floor(Math.random() * 16)] +
               colorRange[Math.floor(Math.random() * 16)] +
               colorRange[Math.floor(Math.random() * 16)];
     };
     module.exports = util;
});

base添加规则

  • 完整的绝对路径 不会加base

  • 以 "." 开头 会相对于当前(被调用的)模块解析地址。 如果不存在被调用的模块(如seajs.use() ), 则会相对于当前页面解析地址。

  • 以 "/" 开头 相对于当前页面的根目录 解析地址

  • 普通命名 直接加上base前缀

base值
  • base 默认值是 seajs所在目录

  • seajs.config()中base的解析与ID命名解析规则相同

例如:

http://example.com/test/js/sea/sea.js

http://example.com/test/index.html

在index.html中调用了sea.js

base的默认值为 "http://example.com/test/js/sea/"

如果使用seajs.config()设置了base

seajs.config({
    base: "home"  // base值为 "http://example.com/test/js/sea/home"
});

seajs.confg({
    base: "./home"  // base值为 "http://example.com/test/home"
});

seajs.conifg({
    base: "/home"   // base值为 "http://example.com/home"
});


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务