2011-02-07 37 views
1

JSDeffered非常酷: https://github.com/cho45/jsdeferred/blob/master/test-jsdeferred.js在ActionScript中有沒有很酷的Deferred庫?

我們可以編寫最簡單的異步調用鏈。

next(function() { // this `next` is global function 
    alert("1"); 
}). 
next(function() { // this `next` is Deferred#next 
    alert("2"); 
}). 
next(function() { 
    alert("3"); 
}); 

我們的代碼就是這樣 new Execute1(nextFunction); ...所以通心粉代碼。

在ActionScript中有沒有很酷的Deferred庫? 或您正在使用哪個腳本?

+0

hope [this](https://github.com/komelgman/Compromise)help – komelgman 2011-07-21 14:24:14

回答

1

我想大多數補間庫都會按照你的要求去做。例如TweenLite和TimelineLite(https://www.greensock.com/timelinelite/)應該完美地完成這項工作。

3

自己創建此語法非常簡單。每個函數都應該返回類本身的實例(返回這個)。

創建一個名爲Chainer

package 
{ 
    public class Chainer 
    { 
     public static function create():Chainer 
     { 
      return new Chainer(); 
     } 

     public function next(func:Function, ...rest):Chainer 
     { 
      func.call(this, rest); // call the function with params 
      return this; // returns itself to enable chaing 
     } 
    } 

} 

現在使用類與下一代功能的AS3類。你可以這樣調用它:

Chainer.create() 
    .next(function():void { 
     trace("1") 
    }) 
    .next(function():void { 
     trace("2") 
    }); 

可能有問題,如果你想擴展Chainer類,因爲你不能改變返回類型:
OOP problem: Extending a class, override functions and jQuery-like syntax

我已經使用這種類型的代碼創建一個小的輔助類:
http://blog.stroep.nl/2010/10/chain-tween/
http://blog.stroep.nl/2009/11/delayed-function-calling-chain/

BTW這個吐溫庫是基於jQuery的語法過於:
http://code.google.com/p/eaze-tween/

4

我只是碰到這種傳來:

https://github.com/CodeCatalyst/promise-as3

我還沒有嘗試過,但它看起來很有前途。它模仿jQuery的Deferred,遵循CommonJS Promise/A規範(我假設),並且有一套體面的單元測試。

相關問題