代码之家  ›  专栏  ›  技术社区  ›  sehugg

在httpService中使用时,flex属性的行为异常

  •  0
  • sehugg  · 技术社区  · 17 年前

    我正在使用REST请求编写一个Flex应用程序,并试图避免HTTP缓存以及同步客户机/服务器时间。为此,我创建了一个 timestamp 财产:

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

    (The 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
    

    看看时间戳都是怎样的?真奇怪。我希望它每次都对属性进行评估,就像对可绑定变量那样。

    (实际上,我只是再次检查了一下,它对可绑定变量做了同样的事情。但不是所有的客户。闪存的某些版本是否有“问题”?

    2 回复  |  直到 17 年前
        1
  •  1
  •   Joel Hooks    17 年前

    所以这是一个只读的getter?绑定不会更新httpService组件中的时间戳,因为它没有要绑定到的属性。timestamp是函数的输出(如Christopher下面提到的),它不是可绑定属性。您需要创建一个可绑定属性,或者使用当前时间戳显式设置URL,从而避免绑定。

    在您的代码中,您正在使用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();
    }
    

    从而避免任何约束问题…

        2
  •  1
  •   Sean Clark Hess    17 年前

    您可以做的另一件事是使get函数可绑定到特定事件。

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