• 【设为首页】
  • 【收藏闪客居】
当前位置:主页>FLASH AS 编程>AS进阶篇>文章内容
  • ActionScript处理异步事件(三)
  • 来源: 作者: 2008-03-26 【
原文地址:http://kuwamoto.org/2006/05/19/dealing-with-asynchronous-events-part-3/
作者:Sho Kuwamoto
译者:Dreamer

我想要谈论一下Flex 2中对RPC呼叫机制的一些改进。如果不是Matt Chotin友好地告诉我Flex 2中的RPC呼叫已经正式改为返回AsyncToken类型的对象,我可能永远都不会知道,这再一次证明了关于Flex他比我知道的多。

使用 AsyncToken 和 Responders

为了使用as-is机制,你要象这样做:
public function getAlbumInfo(albumId: int) : void    
  1. {    
  2.   // Initiate the call.    
  3.   myService.request = { type: "album", albumId: albumId };    
  4.   var call : AsyncToken = myService.send();    
  5.   // Save the albumId for use later.    
  6.   call.albumId = albumId;    
  7.   // Wire up the handler.    
  8.   call.responder = new Responder(getAlbumInfoResult, null);    
  9. }    
  10. public function getAlbumInfoResult(event: Event) : void    
  11. {    
  12.   // Deal with the results of the previous call.    
  13.   var artistId: int = event.result.album.artistId;    
  14.   myAlbumList.addAlbum(event.call.albumId, event.result.album);    
  15.   // Initiate the next call.    
  16.   myService.request = { type: "artist", artistId : artistId };    
  17.   var call : AsyncToken = myService.send();    
  18.   // Save the artistId for use later.   
  19.   call.artistId = artistId;    
  20.   // Wire up the handler.    
  21.   call.responder = new Responder(getArtistInfoResult, null);    
  22. }    
  23. public function getArtistInfoResult(event: Event) : void    
  24. {    
  25.   // Handle the results of the second call.    
  26.   myArtistList.addArtist(event.call.artistId, event.result.artist);    
  27. }   

你会发现它与上一篇文章中的第一个例子非常相似。 最主要的区别就是现在我们 在framework里而不是在你的代码中实现处理器(handler)。
如果我们有匿名的内部类

如果ActionScript有匿名的内部类,你可以象这样压缩它,就像早先我提到的closure方法一样:

public function getAlbumInfo(albumId: int) : void    
  1. {    
  2.   var call: AsyncToken;    
  3.   // Initiate the first call.    
  4.   myService.request = { type: "album", albumId: albumId };    
  5.   call = myService.send();    
  6.   call.responder = new IResponder()    
  7.   {    
  8.     public function result(data:Object):void    
  9.     {    
  10.       // Handle the results of the first call.    
  11.       var artistId: int = event.result.album.artistId;    
  12.       // Notice we can use albumId here directly.    
  13.       myAlbumList.addAlbum(albumId, event.result.album);    
  14.       // Initiate the second call.    
  15.       myService.request = { type: "artist", artistId: artistId };    
  16.       call = myService.send();    
  17.       call.responder = new IResponder()    
  18.       {   
  19.         public function result(data:Object):void    
  20.         {    
  21.           // Handle the results of the second call.    
  22.           myArtistList.addArtist(artistId,event.result.artist);    
  23.         }    
  24.       }    
  25.     }    
  26.   }    
  27. }   

即使使用了匿名的内部类(ActionScript 并没有这个东西),我还是觉得它不如 closure 方法易读性高,所以我想我会一直使用osure方法。




上一篇:ActionScript处理异步事件(二)   下一篇:没有了
您的评论
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
相关内容

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)