JS非同步編程三:Jscex無創痕切入jQUI

來源:互聯網
上載者:User

很多人可能會有個疑問,Jscex和其他類庫共同使用或者文法混用時,會不會出現衝突或者異常?那麼我們就來做個嘗試吧!

我們使用tab外掛程式:

  1. $(function () {  
  2. $("#tabs").tabs({ event: "mouseover" });  
  3. });  

後來,官網幫這個tab外掛程式擴充了一個自動切換的功能,只需要這樣寫就行:

  1. $(function ()   
  2. {var t = $("#tabs").tabs();  
  3. t.tabs("rotate", 3000, false);  
  4. }); 

擴充的代碼如下:

  1. $.extend($.ui.tabs.prototype, {  
  2.             rotation: null,  
  3.             rotate: function (ms, continuing) {  
  4.                 var self = this,  
  5. o = this.options;  
  6.                 var rotate = self._rotate || (self._rotate = function (e) {  
  7.                     clearTimeout(self.rotation);  
  8.                     self.rotation = setTimeout(function () {  
  9.                         var t = o.selected;  
  10.                         self.select(++t < self.anchors.length ? t : 0);  
  11.                     }, ms);  
  12.                     if (e) {  
  13.                         e.stopPropagation();  
  14.                     }  
  15.                 });  
  16.                 var stop = self._unrotate || (self._unrotate = !continuing  
  17. ? function (e) {  
  18.     if (e.clientX) { // in case of a true click  
  19.         self.rotate(null);  
  20.     }  
  21. }  
  22. : function (e) {  
  23.     t = o.selected;  
  24.     rotate();  
  25. });  
  26.                 // start rotation  
  27.                 if (ms) {  
  28.                     this.element.bind("tabsshow", rotate);  
  29.                     this.anchors.bind(o.event + ".tabs", stop);  
  30.                     rotate();  
  31.                     // stop rotation  
  32.                 } else {  
  33.                     clearTimeout(self.rotation);  
  34.                     this.element.unbind("tabsshow", rotate);  
  35.                     this.anchors.unbind(o.event + ".tabs", stop);  
  36.                     delete this._rotate;  
  37.                     delete this._unrotate;  
  38.                 }  
  39.                 return this;  
  40.             }  
  41.         }); 

依然是那麼費解的代碼!在官方沒有擴充之前,我們可以用Jscex介樣子實現:

  1. var swicthAsync = eval(Jscex.compile("async", function () {  
  2.             var tabCount = $("#tabs ul li").length;  
  3.             while (true) {  
  4.                 for (var i = 0; i < tabCount; i++) {  
  5.                     $await(Jscex.Async.sleep(2000));  
  6.                     $('#tabs').tabs({ selected: i });  
  7.                 }  
  8.             }  
  9.         }));  
  10.         $(function () {  
  11.             $("#tabs").tabs();  
  12.             swicthAsync().start();  
  13.         }); 

可以看得出來,這樣的話Jscex沒有對JqueryUI做任何介入,Jscex只是外部控制的一層殼。這樣無法對測試出Jscex是否能與衝突或者異常,那麼,我們就來用Jscex重寫官方的擴充方法吧!

  1. $.extend($.ui.tabs.prototype, {  
  2.             rotation: null,  
  3.             rotate: function (ms, continuing) {  
  4.                 var self = this,  
  5. o = this.options;  
  6.                 var swicthAsync = eval(Jscex.compile("async", function () {  
  7.                     while (true) {  
  8.                         for (var i = 0; i < self.anchors.length; i++) {  
  9.                             $await(Jscex.Async.sleep(ms));  
  10.                             self.select(i);  
  11.                         }  
  12.                     }  
  13.                 }));  
  14.                 swicthAsync().start();  
  15.                 return this;  
  16.             }  
  17.         }); 

但是continuing參數暫時沒有起作用,該參數是決定使用者在選中後是否繼續迴圈下去,這個就留個大家自己去完善吧~~~~

新的Jscex 庫,請上https://github.com/JeffreyZhao/jscex或者http://www.sndacode.com/projects/jscex/wiki下載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.