phonebook.mxml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3. backgroundColor="white"
  4. backgroundGradientAlphas="0"
  5. backgroundGradientColors="0"
  6. layout="vertical" creationComplete="initApp()">
  7. <mx:states>
  8. <mx:State name="grid">
  9. <mx:RemoveChild target="{loginForm}"/>
  10. <mx:AddChild relativeTo="{panel1}" position="lastChild">
  11. <mx:HBox id="hbox1">
  12. <mx:DataGrid id="contactList" dataProvider="{ws.getContacts.lastResult}" width="350" click="currentState='details'">
  13. <mx:columns>
  14. <mx:DataGridColumn headerText="Name" width="175" dataField="name"/>
  15. <mx:DataGridColumn headerText="Phone" dataField="phone"/>
  16. </mx:columns>
  17. </mx:DataGrid>
  18. </mx:HBox>
  19. </mx:AddChild>
  20. <mx:SetProperty target="{newButton}" name="visible" value="true"/>
  21. <mx:SetProperty target="{deleteButton}" name="visible" value="true"/>
  22. <mx:SetProperty target="{controlbar1}" name="height"/>
  23. </mx:State>
  24. <mx:State name="details" basedOn="grid">
  25. <mx:AddChild relativeTo="{hbox1}" position="lastChild">
  26. <mx:Form>
  27. <mx:FormItem label="Name:">
  28. <mx:TextInput id="nameEdit" text="{contactList.selectedItem.name}"/>
  29. </mx:FormItem>
  30. <mx:FormItem label="Phone:">
  31. <mx:TextInput id="phoneEdit" text="{contactList.selectedItem.phone}" />
  32. <mx:Button id="saveButton" label="Save" click="doSave()" />
  33. </mx:FormItem>
  34. </mx:Form>
  35. </mx:AddChild>
  36. </mx:State>
  37. </mx:states>
  38. <mx:PhoneNumberValidator id="val1" source="{phoneEdit}" property="text" />
  39. <mx:StringValidator id="val2" source="{nameEdit}" property="text" minLength="3" maxLength="20" />
  40. <mx:Validator id="val3" source="{usernameEdit}" property="text" />
  41. <mx:Validator id="val4" source="{passwordEdit}" property="text" />
  42. <mx:WebService id="ws" fault="dataError(event)">
  43. <mx:operation name="saveContact" result="ws.getContacts()" />
  44. <mx:operation name="deleteContact" result="ws.getContacts()" />
  45. <mx:operation name="login" result="loginReturned(event)" />
  46. </mx:WebService>
  47. <mx:Script>
  48. <![CDATA[
  49. import mx.rpc.events.ResultEvent;
  50. import mx.validators.Validator;
  51. import mx.controls.Alert;
  52. import mx.rpc.events.FaultEvent;
  53. private function initApp():void
  54. {
  55. ws.loadWSDL(application.parameters.wsdl);
  56. }
  57. private function dataError(event:FaultEvent):void
  58. {
  59. Alert.show(event.fault.faultString,"Data Communication Error");
  60. }
  61. private function doSave():void
  62. {
  63. if(Validator.validateAll([val1, val2]).length == 0)
  64. {
  65. var data:Object = new Object;
  66. data.id = contactList.selectedItem != null ? contactList.selectedItem.id : -1;
  67. data.name = nameEdit.text;
  68. data.phone = phoneEdit.text;
  69. ws.saveContact(data);
  70. currentState='grid';
  71. }
  72. }
  73. private function doDelete():void
  74. {
  75. ws.deleteContact(contactList.selectedItem.id);
  76. currentState='grid';
  77. }
  78. private function doLogin():void
  79. {
  80. if(Validator.validateAll([val3, val4]).length == 0)
  81. {
  82. ws.login(usernameEdit.text, passwordEdit.text);
  83. usernameEdit.enabled=false;
  84. passwordEdit.enabled=false;
  85. loginProgress.visible=true;
  86. }
  87. }
  88. private function loginReturned(event:ResultEvent):void
  89. {
  90. if(!event.result)
  91. {
  92. usernameEdit.enabled=true;
  93. passwordEdit.enabled=true;
  94. loginProgress.visible=false;
  95. Alert.show("Username and password did not match!", "Login Error");
  96. }
  97. else
  98. {
  99. ws.getContacts();
  100. currentState='grid';
  101. }
  102. }
  103. ]]>
  104. </mx:Script>
  105. <mx:Panel layout="vertical" title="Yii Phone Book" id="panel1" resizeEffect="Resize">
  106. <mx:Form id="loginForm">
  107. <mx:FormItem label="Username:">
  108. <mx:TextInput id="usernameEdit" />
  109. </mx:FormItem>
  110. <mx:FormItem label="Password:">
  111. <mx:TextInput id="passwordEdit" displayAsPassword="true"/>
  112. <mx:Label text="Use demo/demo to login" color="#999999" />
  113. </mx:FormItem>
  114. <mx:FormItem>
  115. <mx:Button label="Login" click="doLogin()" id="button1"/>
  116. </mx:FormItem>
  117. <mx:ProgressBar width="100%" indeterminate="true" id="loginProgress" showEffect="Fade" hideEffect="Fade"
  118. labelPlacement="center" label="Verifying..." color="#ffffff" visible="false"/>
  119. </mx:Form>
  120. <mx:ControlBar id="controlbar1">
  121. <mx:Button label="New" click="contactList.selectedIndex=-1; currentState='details'" id="newButton" visible="false" />
  122. <mx:Button label="Delete" enabled="{contactList.selectedIndex >= 0}" click="doDelete()" id="deleteButton" visible="false" />
  123. </mx:ControlBar>
  124. </mx:Panel>
  125. </mx:Application>