JavaScript asynchronous programming: Jscex cutting-In jQueryUI without any trace

Source: Internet
Author: User

We use the tab plug-in:

 
 
  1. <script> 
  2. $(function () {  
  3. $("#tabs").tabs({ event: "mouseover" });  
  4. });  
  5. </script> 

Effect: You can switch the tab when you hover the mouse over it.

Jscex is short for JavaScript Computation EXpressions. It provides a monadic extension for the JavaScript language, which can significantly improve programming experience in some common scenarios. The Jscex project is fully written in JavaScript and can be used in any execution engine supporting ECMAScript 3, including various browsers and server-side JavaScript environments such as Node. js ).

Currently, Jscex mainly includes the following functions:

JIT compiler: It dynamically compiles code at runtime and is mainly used in the development environment.

AOT compiler: Compile the code statically before execution. Static compiled code can be executed without the JIT compiler, so it is mainly used in the production environment.

Asynchronous programming library: The monadic code generated based on Jscex greatly simplifies asynchronous programming in JavaScript.

The importance of asynchronous programming is self-evident, especially for JavaScript. JavaScript does not provide any mechanism to block code execution. Any "time-consuming" operation must be written in asynchronous mode. The traditional asynchronous operation will return the result through the callback function upon completion. We can perform the following work in the callback function.

But this is also the main cause of the difficulty in asynchronous programming. We have always been used to writing logic in a "linear" way, but the large number of callbacks produced by asynchronous operations break down our algorithms. We cannot use if to implement logical branches, nor use while/for/do to implement loops. Not to mention the combination between asynchronous operations, error handling, and cancellation operations.

Jscex and its asynchronous programming library were born to solve these difficulties.

Jscex is short for JavaScript Computation EXpressions. It provides a monadic extension for the JavaScript language, which can significantly improve programming experience in some common scenarios. The Jscex project is fully written in JavaScript and can be used in any execution engine supporting ECMAScript 3, including various browsers and server-side JavaScript environments (such as Node. js ).

Currently, Jscex mainly includes the following functions:

JIT compiler: It dynamically compiles code at runtime and is mainly used in the development environment.

AOT compiler: Compile the code statically before execution. Static compiled code can be executed without the JIT compiler, so it is mainly used in the production environment.

Asynchronous programming library: The monadic code generated based on Jscex greatly simplifies asynchronous programming in JavaScript.

The importance of asynchronous programming is self-evident, especially for JavaScript. JavaScript does not provide any mechanism to block code execution. Any "time-consuming" operation must be written in asynchronous mode. The traditional asynchronous operation will return the result through the callback function upon completion. We can perform the following work in the callback function.

But this is also the main cause of the difficulty in asynchronous programming. We have always been used to writing logic in a "linear" way, but the large number of callbacks produced by asynchronous operations break down our algorithms. We cannot use if to implement logical branches, nor use while/for/do to implement loops. Not to mention the combination between asynchronous operations, error handling, and cancellation operations.

Jscex and its asynchronous programming library were born to solve these difficulties.

Later, the official website helped the tab plug-in expand a function of automatic switch, just write it like this:

 
 
  1.  <script type="text/javascript"> 
  2. $(function () {  
  3. var t = $("#tabs").tabs();  
  4. t.tabs("rotate", 3000, false);  
  5. });  
  6. </script> 

The extension code is as follows:

 
 
  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. });  
  42. })(jQuery); 

Still so confusing code! Before official expansion, we can use Jscex to describe:

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

As you can see, Jscex does not intervene in JqueryUI. Jscex is only a shell of external control. In this way, we cannot test whether Jscex can conflict with or be abnormal. Therefore, we can use Jscex to rewrite the official extension method!

 
 
  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. });  
  18. })(jQuery); 

The running effect is as follows: everything is normal! However, the continuing parameter does not take effect for the moment. This parameter determines whether the user can continue the loop after the user selects the parameter. You can complete this parameter by yourself ~~~~

For the latest Jscex library, please upload ····

Original article: http://www.cnblogs.com/iamzhanglei/archive/2011/08/21/2148628.html

Series of articles]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.