2010-05-07 101 views
3

一個的AsyncToken回用香菜, 我有過一個 訪問服務[命令(選擇= '清單')] 公共職能getRssFeed(MSG:RssEvent)的AsyncToken { 返回service.list()asyncToken; }如何僞造在ActionScript 3

當我指向「真正的」RssService時,一切都按預期工作。我的問題是當我指向「模擬」RssService。我無法弄清楚如何用一些虛擬數據返回來僞造一個AsyncToken ...有沒有人知道如何做到這一點?

回答

4

解決..............;)

public function list():AsyncToken 

    var rssFeed:Array = [rss,rss,rss]; 
    var token:AsyncToken = createToken(rssFeed); 
    token.addResponder(new AsyncResponder(resultHandler, null)); 
    return token; 

    } 

    private function resultHandler(event:ResultEvent, token:AsyncToken = null):void 
    { 
    event.token.dispatchEvent(event); 
    } 


    protected function createToken(result:Object):AsyncToken 
    { 
    var token:AsyncToken = new AsyncToken(null); 
    setTimeout(applyResult, Math.random()*500, token, result); 
    return token; 
    } 

    private function applyResult(token:AsyncToken, result:Object):void 
    { 
    mx_internal:token.setResult(result); 
    var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token); 
    mx_internal:token.applyResult(event); 
    trace(token); 
    } 
+2

確保添加「use namespace mx_internal;」到你的模擬服務。在下面的答案中向Tim提供信貸。 – mbseid 2012-01-19 14:44:34

1

不要忘了補充:

use namespace mx_internal;

否則你會得到這個異常。

[故障]異常,信息= TypeError:錯誤#1006:setResult不是函數。

1

我使用某種一個ServiceProxy圖案,其提供基本上3種方法:

  • 的invokeOperation
  • mockResultInvokeOperation
  • mockFaultInvokeOperation

我以下列方式延伸用於ServiceProxy:

public class SomeService extends ServiceProxy { 
    public var useMock:Boolean; 

    public function someServiceCall(arg1:Type, arg2:Type, responder:IResponder):AsyncToken { 
     if (useMock) { 
      mockResultInvokeOperation({some fake result object}, responder); 
     } 

     return invokeOperation("someServiceCall", [arg1, arg2], responder); 
    } 

} 

技術上我使用和你完全相同的技巧。

package com.obecto.services.proxy 
{ 
import flash.utils.setTimeout; 

import mx.core.mx_internal; 
import mx.messaging.messages.RemotingMessage; 
import mx.rpc.AbstractOperation; 
import mx.rpc.AbstractService; 
import mx.rpc.AsyncToken; 
import mx.rpc.Fault; 
import mx.rpc.IResponder; 
import mx.rpc.events.FaultEvent; 
import mx.rpc.events.ResultEvent; 

public class ServiceProxy 
{ 
    public var service:AbstractService; 

    public function invokeOperation(operationName:String, arguments:Array, responder:IResponder):AsyncToken 
    { 
     var operation:AbstractOperation = service.getOperation(operationName); 
     operation.arguments = arguments; 

     var token:AsyncToken = operation.send(); 
     token.addResponder(responder); 
     return token; 
    } 

    public function mockResultInvokeOperation(mockResult:Object, responder:IResponder):AsyncToken 
    { 
     var fakeMessage:RemotingMessage = new RemotingMessage(); 

     var token:AsyncToken = new AsyncToken(fakeMessage); 
     token.addResponder(responder); 

     setTimeout(
      function(e:ResultEvent = null):void 
      { 
       token.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, mockResult)); 
      }, 1000); 

     return token; 
    } 

    public function mockFaultInvokeOperation(message:String, responder:IResponder):AsyncToken 
    { 
     var fakeMessage:RemotingMessage = new RemotingMessage(); 

     var token:AsyncToken = new AsyncToken(fakeMessage); 
     token.addResponder(responder); 

     setTimeout(
      function(e:ResultEvent = null):void 
      { 
       token.mx_internal::applyFault(new FaultEvent(FaultEvent.FAULT, false, true, 
        new Fault("MOCK_FAULT", message))); 
      }, 1000); 

     return token; 
    } 

} 
} 
3

使用香菜3.0,你有spicefactory異步命令一個更好的選擇:

public class MockCommand 
{ 
    public var callback:Function; 

    public function execute():void 
    { 
     var timer:Timer = new Timer(500, 1); 
     timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_completeHandler); 
     timer.start(); 
    } 

    private function timer_completeHandler(event:TimerEvent):void 
    { 
     callback(mockResultData); 
    } 
} 

無需mx_internal進口。