2013-04-01 34 views
0

我需要在服務器端進行一些耗費的計算(例如DB查詢和數據分析)。結果需要在瀏覽器中打印。爲了這些目的,我將Future結果從服務器發送到客戶端(立即加載網頁並逐漸從服務器打印未來結果)。例如,在客戶端上的如何在客戶端使用scala.concurrent.Future?

@import scala.concurrent.ExecutionContext.Implicits.global 
@main{ 
    @futureResult.onSuccess{ case res => 
    @println("This line is printed in console: "+res); 
    <div>Any html code is NOT printed in browser</div> 
    } 
    Future result is NOT posted 
} 

在服務器CONSOL我們有服務器端

import scala.concurrent.Future 
import scala.concurrent.ExecutionContext.Implicits.global 
def futureResult = Future { 
    val cc = ConsumingCalculations(); 
    "some result" 
} 

:「此行被打印在控制檯:一些結果」

但在瀏覽器我們只有:「未來的結果不會發布」

Play 2.1,scala 2.10目前正在使用。什麼可能是錯的,有什麼想法嗎?

回答

3

未來不能在客戶端發送,它必須在顯示給客戶端之前在服務器端解析。

經典爲例爲你的未來的結果映射在你的控制器

def myAction = Action { 
    Async { 
     futureResult.map(result => 
      Ok(views.html.myView(result)) 
     ) 
    } 
} 

且模板中,使用的結果,而不是未來。

+0

謝謝!這很有用。 – krispo