標籤:
初試seajs,為下次項目的實戰做準備:
html:
seajsDemo.html
<!DOCTYPE html><html><head><meta charset="utf-8"><title>seajsdemo</title><script src="../js/seajs2.2.0/sea.js"></script><script src="../js/seajs2.2.0/sea-debug.js"></script></head><body><div class="wrap"><span>使用seajs載入js</span><div class="content">內容</div></div></body><script>var app = {};app.debug = false;window.app.controller = "demo1";seajs.config({base:"../js/seaDemo",alias:{"jQuery":"jquery.js"},paths:{"jQuery":"jquery.js"}});seajs.use("../js/seaDemo/app.js");</script><!--seajs入口--></html>
js:
app.js
/***seajs入口*調用其他程式*/define(["jQuery"],function(require,exports,module){ // window.$ = window.jQuery = require("jQuery"); exports.load = function(name){ require.async("../seaDemo/"+name+".js",function(controller){if(typeof(controller.run) =="function"){controller.run(); //運行需要載入的js}})}/******/window.app.load = exports.load;if(window.app.controller){exports.load(window.app.controller);}/***調用載入**/});
demo1.js
define(function(require,exports,module){exports.run = function(){alert("已調用demo1.js"); $(".content").text("hello seajs");}});
目錄結構:
html位置:demo/html/seajsDemo.html
seajs使用demo