使用Flex+Cairngorm+AIR制作列车时刻表查询工具[连载三]

然后我们来建立命令,分别由按照列车车次检索和目的地检索的按钮事件来调用,然后在前端控制器中配置:

编写命令

GetStationDetailCommand.as

 

  
    
  1. package com.example.command
  2. {
  3. import com.adobe.cairngorm.commands.ICommand;
  4. import com.adobe.cairngorm.control.CairngormEvent;
  5. import com.example.business.TrainDelegateWS;
  6. import com.example.business.TrainDelegateDB;
  7. import com.example.event.SearchByStationEvent;
  8. import com.example.model.TrainModelLocator;
  9.  
  10. import mx.collections.ArrayCollection;
  11. import mx.controls.Alert;
  12. import mx.rpc.IResponder;
  13. import mx.rpc.events.FaultEvent;
  14.  
  15. public class GetStationDetailCommand implements ICommand, IResponder
  16. {
  17. private var modelLocator :TrainModelLocator = TrainModelLocator.getInstance ( );
  18. private var deaultNS : Namespace = new Namespace ( "http://WebXml.com.cn/" );
  19. private var diffNS : Namespace = new Namespace ( "diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1" );
  20. public function execute ( event : CairngormEvent ) : void {
  21. var searchEvent :SearchByStationEvent = SearchByStationEvent (event );
  22. modelLocator.startStation = searchEvent. start;
  23. modelLocator.arriveStation = searchEvent.end;
  24. switch (modelLocator.currentService ) {
  25. case "webService" :
  26. var delegate : TrainDelegateWS = new TrainDelegateWS ( this );
  27. delegate.getStationDetail ( );
  28. break;
  29. case "dataBase" :
  30. var delegateDB : TrainDelegateDB = new TrainDelegateDB ( this );
  31. delegateDB.getStationDetail ( );
  32. break;
  33. }
  34. }
  35.  
  36. public function result ( event : Object ) : void {
  37. default xml namespace = deaultNS;
  38. var resultXML : XML = new XML (event.result. toString ( ) );
  39. var resultList : XMLList = resultXML.getStationAndTimeByStationNameResult.diffNS ::diffgram.getStationAndTime.TimeTable;
  40. var model : TrainModelLocator = TrainModelLocator.getInstance ( );
  41. model.currentStaionList.trainStationList = new ArrayCollection ( );
  42. var count : uint = 1;
  43. for each ( var item : XML in resultList ) {
  44. var nodeItem : Object = new Object ( );
  45. nodeItem.num = count;
  46. count ++;
  47. nodeItem.TrainCode = item.TrainCode;
  48. nodeItem.FirstStation = item.FirstStation;
  49. nodeItem.LastStation = item.LastStation;
  50. nodeItem.StartStation = item.StartStation;
  51. nodeItem.StartTime = item.StartTime;
  52. nodeItem.ArriveStation = item.ArriveStation;
  53. nodeItem.ArriveTime = item.ArriveTime;
  54. nodeItem.KM = item.KM;
  55. nodeItem.UseDate = item.UseDate;
  56. model.currentStaionList.trainStationList.addItem (nodeItem );
  57. }
  58. model.currentState = "resultByStationList";
  59. }
  60.  
  61. public function fault ( event : Object ) : void {
  62. var faultEvent : FaultEvent = FaultEvent ( event );
  63. Alert. show ( "Train could not be retrieved!" );
  64. }
  65.  
  66. }
  67. }

 

GetTrainDetailCommand.as

 

  
    
  1. package com.example.command
  2. {
  3. import com.adobe.cairngorm.commands.ICommand;
  4. import com.adobe.cairngorm.control.CairngormEvent;
  5. import com.example.business.TrainDelegateWS;
  6. import com.example.business.TrainDelegateDB;
  7. import com.example.event.SearchByTrainNameEvent;
  8. import com.example.model.TrainModelLocator;
  9.  
  10. import mx.collections.ArrayCollection;
  11. import mx.controls.Alert;
  12. import mx.rpc.IResponder;
  13. import mx.rpc.events.FaultEvent;
  14.  
  15. public class GetTrainDetailCommand implements ICommand, IResponder
  16. {
  17. private var modelLocator :TrainModelLocator = TrainModelLocator.getInstance ( );
  18. private var deaultNS : Namespace = new Namespace ( "http://WebXml.com.cn/" );
  19. private var diffNS : Namespace = new Namespace ( "diffgr", "urn:schemas-microsoft-com:xml-diffgram-v1" );
  20. public function execute ( event : CairngormEvent ) : void {
  21. var searchEvent :SearchByTrainNameEvent = SearchByTrainNameEvent (event );
  22. modelLocator.currentTrain. name = searchEvent.trainName;
  23. switch (modelLocator.currentService ) {
  24. case "webService" :
  25. var delegate : TrainDelegateWS = new TrainDelegateWS ( this );
  26. delegate.getTrainDetail ( );
  27. break;
  28. case "dataBase" :
  29. var delegateDB : TrainDelegateDB = new TrainDelegateDB ( this );
  30. delegateDB.getTrainDetail ( );
  31. break;
  32. }
  33. }
  34.  
  35. public function result ( event : Object ) : void {
  36. default xml namespace = deaultNS;
  37. var resultXML : XML = new XML (event.result. toString ( ) );
  38. var resultList : XMLList = resultXML.getDetailInfoByTrainCodeResult.diffNS ::diffgram.getDetailInfo.TrainDetailInfo;
  39.  
  40. var model : TrainModelLocator = TrainModelLocator.getInstance ( );
  41. model.currentTrain.journey = new ArrayCollection ( );
  42. var count : uint = 1;
  43. for each ( var item : XML in resultList ) {
  44. var nodeItem : Object = new Object ( );
  45. nodeItem.num = count;
  46. count ++;
  47. nodeItem.TrainStation = item.TrainStation;
  48. nodeItem.ArriveTime = item.ArriveTime;
  49. nodeItem.StartTime = item.StartTime;
  50. nodeItem.KM = item.KM;
  51. model.currentTrain.journey.addItem (nodeItem );
  52. }
  53. model.currentState = "resultByTrainName";
  54. }
  55.  
  56. public function fault ( event : Object ) : void {
  57. var faultEvent : FaultEvent = FaultEvent ( event );
  58. Alert. show ( "Train could not be retrieved!" );
  59. }
  60.  
  61. }
  62. }

 

前端控制器

然后让我们来完成前端控制器,这里比较简单,就是映射两个事件到相应的命令上:

TrainController.as

 

  
    
  1. package com.example.control
  2. {
  3. import com.adobe.cairngorm.control.FrontController;
  4. import com.example.command. *;
  5. import com.example.event.SearchByTrainNameEvent;
  6. import com.example.event.SearchByStationEvent;
  7.  
  8. public class TrainController extends FrontController
  9. {
  10. public function TrainController ( ) {
  11. initialiseCommands ( );
  12. }
  13.  
  14. public function initialiseCommands ( ) : void {
  15. addCommand ( SearchByTrainNameEvent.SEARCH_BY_TRAIN_NAME, GetTrainDetailCommand );
  16. addCommand ( SearchByStationEvent.SEARCH_BY_STATION, GetStationDetailCommand );
  17. }
  18.  
  19. }
  20. }

 

自定义事件

我们需要自定义事件的播放来触发命令的操作,这里我们建立两个自定义事件,分别对应两种搜索状态:

SearchByStationEvent.as

 

  
    
  1. package com.example.event
  2. {
  3. import flash.events. Event;
  4. import com.adobe.cairngorm.control.CairngormEvent;
  5. public class SearchByStationEvent extends CairngormEvent {
  6.  
  7. public static var SEARCH_BY_STATION : String = "searchByStation";
  8.  
  9. public var start : String;
  10. public var end : String;
  11.  
  12. public function SearchByStationEvent ( ) {
  13. super (SEARCH_BY_STATION );
  14. }
  15.  
  16. override public function clone ( ) : Event {
  17. return new SearchByStationEvent ( );
  18. }
  19.  
  20. }
  21. }

 

SearchByTrainNameEvent.as

 

  
    
  1. package com.example.event
  2. {
  3. import flash.events. Event;
  4. import com.adobe.cairngorm.control.CairngormEvent;
  5. public class SearchByTrainNameEvent extends CairngormEvent {
  6.  
  7. public static var SEARCH_BY_TRAIN_NAME : String = "searchByTrainName";
  8.  
  9. public var trainName : String;
  10.  
  11. public function SearchByTrainNameEvent ( ) {
  12. super (SEARCH_BY_TRAIN_NAME );
  13. }
  14.  
  15. override public function clone ( ) : Event {
  16. return new SearchByTrainNameEvent ( );
  17. }
  18.  
  19. }
  20. }

 

下一步实现数据模型:

TrainModelLocator.as

 

  
    
  1. package com.example.model
  2. {
  3. import com.adobe.cairngorm.model.ModelLocator;
  4. import com.example.vo.StationListVO;
  5. import com.example.vo.TrainVO;
  6.  
  7. [Bindable ]
  8. public class TrainModelLocator implements ModelLocator {
  9.  
  10. private static var modelLocator : TrainModelLocator;
  11.  
  12. public static function getInstance ( ) : TrainModelLocator {
  13. if ( modelLocator == null ) {
  14. modelLocator = new TrainModelLocator ( );
  15. }
  16. return modelLocator;
  17. }
  18.  
  19. public function TrainModelLocator ( ) {
  20. if ( modelLocator != null ) {
  21. throw new Error ( "Only one TrainModelLocator instance should be instantiated" );
  22. }
  23. }
  24. //serviceConfig
  25. public var wsWSDL : String = "http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx?wsdl";
  26.  
  27. public var currentTrain : TrainVO = new TrainVO ( );
  28. public var currentStaionList : StationListVO = new StationListVO ( );
  29. public var currentService : String = "webService";
  30. public var startStation : String = "";
  31. public var arriveStation : String = "";
  32. //当前状态
  33. public var currentState : String = "";
  34.  
  35. }
  36. }

 

我们定义两个ValueObject类,用于保存按照两种搜索模式检索的返回结果:

StationListVO.as

 

  
    
  1. package com.example.vo
  2. {
  3. import com.adobe.cairngorm.vo.IValueObject;
  4.  
  5. import mx.collections.ArrayCollection;
  6.  
  7. [RemoteClass (alias= "com.example.vo.StationListVO" ) ]
  8. public class StationListVO implements IValueObject
  9. {
  10. //始发站
  11. [Bindable ]
  12. public var startStation : String;
  13. //目的地
  14. [Bindable ]
  15. public var endStation : String;
  16. //列车以及行程
  17. [Bindable ]
  18. public var trainStationList :ArrayCollection;
  19. }
  20. }

 

TrainVO.as

 

  
    
  1. package com.example.vo
  2. {
  3. import com.adobe.cairngorm.vo.IValueObject;
  4.  
  5. import mx.collections.ArrayCollection;
  6.  
  7. [RemoteClass (alias= "com.example.vo.TrainVO" ) ]
  8. public class TrainVO implements IValueObject
  9. {
  10. //列车车次
  11. [Bindable ]
  12. public var name : String;
  13. //列车行程,是一个数组,数组的每一项包含序号,车站,到达时间,发车时间,里程(公里)
  14. [Bindable ]
  15. public var journey :ArrayCollection;
  16. }
  17. }

 

至此完成了编码工作,然后我们打开AIR的配置文件(TrainSchedule-app.xml),修改宽度和高度为(800*600),这样返回结果可以显示的更全面一些。

源码下载地址:

http://doc.riameeting.com/download/TrainSchedule.rar

你可能感兴趣的:(Flex)