2012-07-21 59 views
3

我在Dart試驗了一些WebGL,並且我創建了一個從單獨文件加載着色器的類,我想在對象準備就緒時拋出一個事件(函數),所以我可以繼續我的應用程序,知道我的着色器是正確加載。有人知道一個簡單的方法來做到這一點嗎?如何在Dart上準備好對象時分派事件?

回答

6

一種方法是使用一個未來模式來實現這一點:

Future<SomeType> initMyObject(){ 
    final c = new Completer(); 

    // Do my object init stuff 
    // and when it is complete: 
    c.complete(instanceOfSomeType); 

    // Return the Future object to any subscribers. 
    return c.future; 
} 

然後在其他地方,你可以得到通知,像這樣:

initMyObject().then((SomeType t){ 
    //executes when future completes 
});