2012-04-06

Yepnope.js 和 CoffeeScript 拆檔 (續)

繼上篇 Yepnope.js 和 CoffeeScript 拆檔 改良一下, 使用 CoffeeScript.load() 裡的 callBack 功能, 讓 *.coffee 可以依序載入並執行。另外, 設置了 isDeveloping 變數, 使得在開發的過程避免 *.coffee cache 的問題。

<<< t04_yepnope.html ----------------------------------->>>
<html>
 <head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <script type="text/javascript" src="js/yepnope.1.5.3-min.js"></script>
  <script type="text/javascript" src="t04_yepnope.js"></script>
 </head>
 <body>
  <div id="buttons" mydata="shinder">
   <button id="b1"> Button 1 </button>
   <button> Button 2 </button>
   <button> Button 3 </button>
  </div>
 </body>
</html> 

<<< t04_yepnope.js ----------------------------------->>>
(function(){
 var coffees_index = 0;
 var isDeveloping = true;
 var coffees = ['t03_yepnope2', 't03_yepnope1', 't03_yepnope3'];

 function runCoffees()
 {
  var num = new Date().getTime();
  if( coffees[coffees_index] ) {
   if(isDeveloping) {
    CoffeeScript.load( coffees[coffees_index] + ".coffee?a" + num, runCoffees);
   } else
    CoffeeScript.load( coffees[coffees_index] + ".coffee", runCoffees);
   coffees_index ++;
  }
 }

 yepnope([
  { load: "js/jquery-1.7.1.js" },
  { load: "js/underscore-1.3.1.js" },
  { test: !JSON, yep:"js/json2_min.js" },
  { load: "js/backbone.js" },
  {
   load: "js/coffee-script.js",
   complete: runCoffees
  }
 ]);
})();

沒有留言:

FB 留言