2009-08-06 27 views
0

我正在使用REST請求編寫一個Flex應用程序,並嘗試避免HTTP緩存以及同步客戶端/服務器時間。爲此我創建了一個timestamp財產如:Flex屬性在HTTPService中使用時表現奇怪

// returns a timestamp corrected for server time    
private function get timestamp() : Number 
{ 
    return new Date().getTime() + clientClockAdjustMsec; 
} 

(該clientClockAdjustMsec我已經使用特殊的魔力已經設置)

我也嘗試包括我的查詢字符串這樣的時間戳:

<mx:HTTPService url="/Service?ts={timestamp}" ... 

但是我在訪問日誌中看到的是奇怪的。這是這樣的:

1.2.3.4 - - [06/Aug/2009:17:19:47 +0000] "GET /Service?ts=1249579062937 HTTP/1.1" 200 478 
1.2.3.4 - - [06/Aug/2009:17:20:13 +0000] "GET /Service?ts=1249579062937 HTTP/1.1" 200 500 
1.2.3.4 - - [06/Aug/2009:17:20:14 +0000] "GET /Service?ts=1249579062937 HTTP/1.1" 200 435 

看看時間戳是如何都是一樣的?這麼奇怪。我期望它每次評估屬性,就像它爲Bindable變量一樣。

(其實,我只是檢查了一遍,它也做同樣的事情可綁定變量。但是,並非所有的客戶。做某些Flash的版本有「問題」?)

回答

1

所以這是一個只讀的getter ?綁定不會更新HTTPService組件中的{timestamp},因爲它沒有要綁定的屬性。時間戳是函數的輸出(正如克里斯托弗在下面提到的)並且不是Bindable屬性。您需要創建可綁定屬性,或者使用當前時間戳顯式設置URL,從而完全避免綁定。

的Someplace您正在使用myService.send()你的代碼,你需要做的是這樣的:

[Bindable] 
private var timestamp:Number; 

private function whereSendHappens():void 
{ 
    timestamp = new Date().getTime() + clientClockAdjustMsec; 
    myService.send() 
} 

<mx:HTTPService url="/Service?ts={timestamp}" ... 

如果由於某種原因,沒有工作:

private function whereSendHappens():void 
{ 
    timestamp = new Date().getTime() + clientClockAdjustMsec; 
    myService.url = "/Service?ts=" + timestamp; 
    myService.send(); 
} 

從而避免任何綁定問題...

+0

喬爾是正確的。因爲你永遠不會告訴你的HTTPService'timestamp'的值已經改變,它不知道更新數據綁定。 – Dan 2009-08-06 20:40:14

+1

* TECHNICALLY *這不是數據綁定,因爲它涉及到函數的輸出值而不是屬性。 – cwallenpoole 2009-08-06 20:59:18

+0

我更新了答案,以反映您的闡述Christopher。謝謝你指出。 – 2009-08-06 21:22:04

1

您可以做的另一件事是讓get函數可綁定到特定的事件。

[Bindable("updateTimestamp")] 
public function get timestamp() : Number { ... } 

public function whereSendHappens():void 
{ 
    dispatchEvent(new Event("updateTimestamp")); // will cause the binding to fire 
    myService.send(); 
}