然后我们来建立命令,分别由按照列车车次检索和目的地检索的按钮事件来调用,然后在前端控制器中配置:
GetStationDetailCommand.as
package com.example.command { import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import com.example.business.TrainDelegateWS; import com.example.business.TrainDelegateDB; import com.example.event.SearchByStationEvent; import com.example.model.TrainModelLocator; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.rpc.IResponder; import mx.rpc.events.FaultEvent; public class GetStationDetailCommand implements ICommand, IResponder { private var modelLocator :TrainModelLocator = TrainModelLocator.getInstance ( ); public function execute ( event : CairngormEvent ) : void { var searchEvent :SearchByStationEvent = SearchByStationEvent (event ); modelLocator.startStation = searchEvent. start; modelLocator.arriveStation = searchEvent.end; switch (modelLocator.currentService ) { case "webService" : var delegate : TrainDelegateWS = new TrainDelegateWS ( this ); delegate.getStationDetail ( ); break; case "dataBase" : var delegateDB : TrainDelegateDB = new TrainDelegateDB ( this ); delegateDB.getStationDetail ( ); break; } } default xml namespace = deaultNS; var resultList : XMLList = resultXML.getStationAndTimeByStationNameResult.diffNS ::diffgram.getStationAndTime.TimeTable; var model : TrainModelLocator = TrainModelLocator.getInstance ( ); model.currentStaionList.trainStationList = new ArrayCollection ( ); nodeItem.num = count; count ++; nodeItem.TrainCode = item.TrainCode; nodeItem.FirstStation = item.FirstStation; nodeItem.LastStation = item.LastStation; nodeItem.StartStation = item.StartStation; nodeItem.StartTime = item.StartTime; nodeItem.ArriveStation = item.ArriveStation; nodeItem.ArriveTime = item.ArriveTime; nodeItem.KM = item.KM; nodeItem.UseDate = item.UseDate; model.currentStaionList.trainStationList.addItem (nodeItem ); } model.currentState = "resultByStationList"; } var faultEvent : FaultEvent = FaultEvent ( event ); Alert. show ( "Train could not be retrieved!" ); } } }
GetTrainDetailCommand.as
package com.example.command { import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent; import com.example.business.TrainDelegateWS; import com.example.business.TrainDelegateDB; import com.example.event.SearchByTrainNameEvent; import com.example.model.TrainModelLocator; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.rpc.IResponder; import mx.rpc.events.FaultEvent; public class GetTrainDetailCommand implements ICommand, IResponder { private var modelLocator :TrainModelLocator = TrainModelLocator.getInstance ( ); public function execute ( event : CairngormEvent ) : void { var searchEvent :SearchByTrainNameEvent = SearchByTrainNameEvent (event ); modelLocator.currentTrain. name = searchEvent.trainName; switch (modelLocator.currentService ) { case "webService" : var delegate : TrainDelegateWS = new TrainDelegateWS ( this ); delegate.getTrainDetail ( ); break; case "dataBase" : var delegateDB : TrainDelegateDB = new TrainDelegateDB ( this ); delegateDB.getTrainDetail ( ); break; } } default xml namespace = deaultNS; var resultList : XMLList = resultXML.getDetailInfoByTrainCodeResult.diffNS ::diffgram.getDetailInfo.TrainDetailInfo; var model : TrainModelLocator = TrainModelLocator.getInstance ( ); model.currentTrain.journey = new ArrayCollection ( ); nodeItem.num = count; count ++; nodeItem.TrainStation = item.TrainStation; nodeItem.ArriveTime = item.ArriveTime; nodeItem.StartTime = item.StartTime; nodeItem.KM = item.KM; model.currentTrain.journey.addItem (nodeItem ); } model.currentState = "resultByTrainName"; } var faultEvent : FaultEvent = FaultEvent ( event ); Alert. show ( "Train could not be retrieved!" ); } } }
然后让我们来完成前端控制器,这里比较简单,就是映射两个事件到相应的命令上:
TrainController.as
package com.example.control { import com.adobe.cairngorm.control.FrontController; import com.example.command. *; import com.example.event.SearchByTrainNameEvent; import com.example.event.SearchByStationEvent; public class TrainController extends FrontController { public function TrainController ( ) { initialiseCommands ( ); } public function initialiseCommands ( ) : void { addCommand ( SearchByTrainNameEvent.SEARCH_BY_TRAIN_NAME, GetTrainDetailCommand ); addCommand ( SearchByStationEvent.SEARCH_BY_STATION, GetStationDetailCommand ); } } }
我们需要自定义事件的播放来触发命令的操作,这里我们建立两个自定义事件,分别对应两种搜索状态:
SearchByStationEvent.as
package com.example.event { import com.adobe.cairngorm.control.CairngormEvent; public class SearchByStationEvent extends CairngormEvent { public function SearchByStationEvent ( ) { super (SEARCH_BY_STATION ); } return new SearchByStationEvent ( ); } } }
SearchByTrainNameEvent.as
package com.example.event { import com.adobe.cairngorm.control.CairngormEvent; public class SearchByTrainNameEvent extends CairngormEvent { public function SearchByTrainNameEvent ( ) { super (SEARCH_BY_TRAIN_NAME ); } return new SearchByTrainNameEvent ( ); } } }
TrainModelLocator.as
package com.example.model { import com.adobe.cairngorm.model.ModelLocator; import com.example.vo.StationListVO; import com.example.vo.TrainVO; [Bindable ] public class TrainModelLocator implements ModelLocator { private static var modelLocator : TrainModelLocator; public static function getInstance ( ) : TrainModelLocator { if ( modelLocator == null ) { modelLocator = new TrainModelLocator ( ); } return modelLocator; } public function TrainModelLocator ( ) { if ( modelLocator != null ) { } } //serviceConfig public var currentTrain : TrainVO = new TrainVO ( ); public var currentStaionList : StationListVO = new StationListVO ( ); //当前状态 } }
我们定义两个ValueObject类,用于保存按照两种搜索模式检索的返回结果:
StationListVO.as
package com.example.vo { import com.adobe.cairngorm.vo.IValueObject; import mx.collections.ArrayCollection; [RemoteClass (alias= "com.example.vo.StationListVO" ) ] public class StationListVO implements IValueObject { //始发站 [Bindable ] //目的地 [Bindable ] //列车以及行程 [Bindable ] public var trainStationList :ArrayCollection; } }
TrainVO.as
package com.example.vo { import com.adobe.cairngorm.vo.IValueObject; import mx.collections.ArrayCollection; [RemoteClass (alias= "com.example.vo.TrainVO" ) ] public class TrainVO implements IValueObject { //列车车次 [Bindable ] //列车行程,是一个数组,数组的每一项包含序号,车站,到达时间,发车时间,里程(公里) [Bindable ] public var journey :ArrayCollection; } }
至此完成了编码工作,然后我们打开AIR的配置文件(TrainSchedule-app.xml),修改宽度和高度为(800*600),这样返回结果可以显示的更全面一些。
源码下载地址: