将 Web 服务结果与 Flex 中的请求匹配 [英] Matching web service results to requests in Flex

查看:24
本文介绍了将 Web 服务结果与 Flex 中的请求匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我回答这个问题之前,先了解一点(!)背景:

A little (!) bit of background before I can get to the question :

我有一个手风琴控件,它加载了一组网格,每个网格都懒加载了一系列的东西.我正在使用自动生成的 Web 服务代理来检索这些列表.我希望用户能够在手风琴中更改选定的子项,而无需等待 Web 服务响应.我最初对所有请求使用相同的代理实例并按照请求的顺序跟踪请求,但问题在于较短的数组从服务器返回的速度更快,因此发出请求的顺序变得无关紧要.

I have an accordion control loaded with an array of grids, each of which is lazy loaded with arrays of things. I'm using an auto-generated web service proxy to retrieve these lists. I'd like for the user to be able to change the selected child in the accordion without having to wait for the web service to respond. I was originally using the same proxy instance for all requests and keeping track of the requests in the order they were made, but the problem with this is that shorter arrays return more quickly from the server, so the order the requests were made in becomes irrelevant.

在处理代理结果事件时,我找不到确定原始请求的明显方法,所以我最终得到的是一个处理手风琴上的更改事件的函数,实例化一个新的 web 服务代理,将其推入一个带有所选子项的索引的哈希表,然后添加一个闭包作为事件处理程序.即有点像这样的东西:

I couldn't find an obvious way to determine the original request when handling a proxy result event so what I ended up with is a function which handles the change event on the accordion, instantiates a new webservice proxy, shoves that into a hashtable with the index of the selected child and then adds a closure as an event handler. i.e. something a bit like this :

private proxyTable:Object = new Object();
private function PopulateThingGrid(index:Number):void
{
    var grid:ThingGrid = myAccordion.getChildAt(index) as ThingGrid;
    grid.things = ArrayCollection(proxyTable[index].getThings_lastResult);
}

private function SendThingRequest(index:int):void
{
    var grid:ThingGrid= myAccordion.getChildAt(index) as ThingGrid;
    if (grid.things.length == 0)
    {
        if (proxyTable[index] == null)
        {
            proxyTable[index] = new MyWebServiceProxy();
        }
        var proxy:MyWebServiceProxy= proxyTable[index];
        proxy.addgetThingsEventListener(function ():void { PopulateThingGrid(index); });

        var list:ThingList = thingLists.getItemAt(index) as ThingList;
        proxy.getThings("thinglist", list.ListID);
    }
}

private function myAccordion_Change(event:IndexChangedEvent):void
{
    SendThingRequest(event.newIndex);
}

(我已经尝试将其匿名化,所以我可能错过了一些东西,但希望你能明白)

(I've tried to anonymise this a bit, so I may have missed something, but hopefully you get the idea)

那么,对于问题:是否有一种更简单的方法可以将代理结果与我刚刚遗漏的原始请求相匹配?

So, to the question(s) : is there an easier way to match up proxy results with the original requests that I'm just missing?

如果不是,我所做的是否合理?我有点担心我最终可以生成并正确处理它们的代理实例的数量(在必要时) - 是否有任何我可能不知道的陷阱?

If not, is what I've done reasonable? I'm a little concerned about the number of proxy instances that I could end up generating and then disposing of them correctly (when that becomes necessary) - are there any pitfalls I might not be aware of?

更新:我认为可能会出现问题,因为生成的代理代码是从 flash.events.Event 继承 ResultEvents,而不是 mx.rpc.events.ResultEvent.我不完全确定它为什么这样做 - 访问 AsyncToken 的唯一方法是在方法调用最初返回它时.

Update : I think the problem may arise because the generated proxy code subclasses the ResultEvents from flash.events.Event, rather than mx.rpc.events.ResultEvent. I'm not entirely sure why it does this - the only way to access the AsyncToken is when it's initially returned by the method call.

推荐答案

不确定这是否有帮助,但我遇到了类似的情况,我有一个 RemoteObject,我在上面调用了 4 个 CRUD 方法,但只有一个 resultHandler.我使用 AsyncToken 解决了这个问题.

Not sure if this helps, but I had a similar situation, where I had a RemoteObject on which I call the 4 CRUD methods, but only one resultHandler. I solved this using the AsyncToken.

我的 RemoteObject 调用如下所示:

My RemoteObject calls looked like this:

public function list() {
    var token:AsyncToken = myService.list();
    token.operation = "list";
}

public function update() {
    var token:AsyncToken = myService.update(selectedItem);
    token.operation = "update";
}

public function create() {
    var token:AsyncToken = myService.create(selectedItem);
    token.operation = "create";
}

public function delete() {
    var token:AsyncToken = myService.delete(selectedItem);
    token.operation = "delete";
}

然后,resultHandler 看起来像这样:

Then, the resultHandler looks like this:

public function resultHandler(event:ResultEvent):void {
    if( event.token.operation == "list" ) {
      // do something
    }   else if( event.token.operation == "update" ) {
    // do something
    }   
    // etc...

operation 是一个动态属性,不是 AsyncToken 的成员,所以你可以随意调用它.有一篇食谱文章描述了它这里:

operation is a dynamic property, not a member of AsyncToken, so you can call it whatever you like. There's a Cookbook article describing it here:

这篇关于将 Web 服务结果与 Flex 中的请求匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆