[flex] Flex与Rails结合的问题

leiting_1985 2009-01-12
在Flex with Rails之本书中的一个例子
TaskCreatBox.mxml
<?xml version="1.0" encoding="utf-8"?>   
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"  
    width="100%" height="100%" label="New Task">   
<mx:Metadata>   
     [Event(name="taskCreate", type="com.pomodo.events.TaskEvent")]   
</mx:Metadata>   
<mx:Script>   
<![CDATA[   
    import mx.rpc.events.ResultEvent;   
    import com.pomodo.events.TaskEvent;   
    private function handleTaskCreateResult(event:ResultEvent):   
      void {   
        var resultXML: XML = XML(event.result);   
        Pomodo.debug("TaskCreateBox#handleTaskCreateResult:\n" +   
               resultXML.toString());   
         dispatchEvent(new TaskEvent(TaskEvent.TASK_CREATE,   
                 resultXML));   
     }   
    private function doTaskCreate():void {   
         svcTasksCreate.send();   
    }   
]]>   
</mx:Script>   
  <mx:HTTPService   
  id="svcTasksCreate"  
  url="/tasks.xml"  
  contentType="application/xml"  
  resultFormat="e4x"  
  method="POST"  
  result="handleTaskCreateResult(event)">   
     <mx:request>   
      <task>   
       <name>{nameTI.text}</name>   
       <notes>{notesTI.text}</notes>   
      </task>   
     </mx:request>   
  </mx:HTTPService>   
  <mx:Form width="100%" height="100%">   
   <mx:FormItem label="Task">   
     <mx:TextInput id="nameTI" width="200"/>   
   </mx:FormItem>   
   <mx:FormItem label="Notes">   
     <mx:TextArea id="notesTI" width="200" height="100"/>   
   </mx:FormItem>   
   <mx:FormItem label="Project">   
     <mx:ComboBox id="projectsCB" width="200"/>   
   </mx:FormItem>   
   <mx:FormItem label="">   
     <mx:CheckBox id="nextActionCheckbox"  
         label="This is the Next Action"/>   
   </mx:FormItem>   
   <mx:FormItem label="Location">   
     <mx:ComboBox id="locationsCB" width="200"/>   
   </mx:FormItem>   
   <mx:FormItem>   
     <mx:Button label="Submit" width="160" height="30"  
        click="doTaskCreate()"/>   
   </mx:FormItem>   
  </mx:Form>   
</mx:VBox> 

TaskListBox.mxml
<?xml version="1.0" encoding="utf-8"?>   
<mx:VDividedBox xmlns:mx="http://www.adobe.com/2006/mxml"  
    width="100%" height="100%" label="Tasks"  
    creationComplete="listTasks()">   
<mx:Script>   
  <![CDATA[   
    import mx.rpc.events.ResultEvent;   
    public const NEXT_ACTIONS:int = 0;   
    public const ALL_TASKS:int = 1;   
    public const TASKS_IN_PROJECT:int = 2;   
    public const TASKS_AT_LOCATION:int = 3;   
    private const SHOW_CHOICES:Array = [   
           {label:"Next Actions", data:NEXT_ACTIONS,   
                   hasSubChoice:false},   
           {label:"All Tasks", data:ALL_TASKS,   
                   hasSubChoice:false},   
           {label:"Tasks in Project:", data:TASKS_IN_PROJECT,   
                   hasSubChoice:true},   
           {label:"Tasks at Location:", data:TASKS_AT_LOCATION,   
                   hasSubChoice:true}];   
   [Bindable]   
   private var _subChoices:Array;   
      
   public function listTasks():void {   
     svcTasksList.send();   
     Pomodo.debug("sendedaaaaaaaaaaaa\n");   
    }   
   private function handleTasksListResult(event:ResultEvent):   
   void {   
     var resultXML: XML = XML(event.result);   
     Pomodo.debug("TasksListBox#handleTasksListResult:\n" +   
        resultXML.toString());   
}   
  ]]>   
</mx:Script>   
  <mx:HTTPService id="svcTasksList"    
    url="/tasks.xml"  
    resultFormat="e4x"  
    result="handleTasksListResult(event)"/>   
  <mx:XMLListCollection id="tasksXLC"  
    source="{XMLList(svcTasksList.lastResult.children())}"/>   
  <mx:VBox width="100%" height="60%">   
   <mx:HBox width="100%" paddingLeft="5" paddingRight="5">   
     <mx:Label text="Show:"/>   
     <mx:ComboBox id="mainChoiceCB" dataProvider="{SHOW_CHOICES}"/>   
     <mx:ComboBox id="subChoiceCB" width="100%"  
        dataProvider="{_subChoices}"  
        visible="{mainChoiceCB.selectedItem.hasSubChoice}"/>   
   </mx:HBox>   
   <mx:DataGrid id="tasksGrid" width="100%" height="100%"  
        dataProvider="{tasksXLC}">   
      <mx:columns>   
         <mx:DataGridColumn headerText="" width="25"  
             dataField="completed"/>   
         <mx:DataGridColumn headerText="Name"  
             width="250" dataField="name"/>   
         <mx:DataGridColumn headerText="Project"  
             width="150" dataField="project_id"/>   
         <mx:DataGridColumn headerText="Location"  
             width="150" dataField="location_id"/>   
         <mx:DataGridColumn headerText="Notes" dataField="notes"/>   
         <mx:DataGridColumn headerText="" width="60"/>   
      </mx:columns>   
    </mx:DataGrid>   
  </mx:VBox>   
  <mx:Panel id="summaryPanel" title="Task" width="100%"  
      height="40%" paddingLeft="5" paddingRight="5"  
      paddingTop="5" paddingBottom="5">   
    <mx:HBox width="100%">   
       <mx:Label text="Name" width="50"/>   
       <mx:TextInput id="nameTI" width="100%"/>   
    </mx:HBox>   
    <mx:HBox width="100%" verticalAlign="middle">   
       <mx:Label text="Project" width="50"/>   
       <mx:ComboBox id="projectCB" width="200"/>   
       <mx:CheckBox label="This is the Next Action"/>   
       <mx:Spacer width="100%"/>   
       <mx:Label text="Location"/>   
       <mx:ComboBox id="locationCB"/>   
    </mx:HBox>   
    <mx:HBox width="100%" height="100%">   
        <mx:Label text="Notes" width="50"/>   
        <mx:TextArea id="notesTI" width="100%"  
         height="100%"/>   
    </mx:HBox>   
    <mx:ControlBar width="100%" horizontalAlign="center">   
       <mx:Button id="updateButton" label="Update"  
        width="100%" height="30"/>   
       <mx:Button id="deleteButton" label="Delete"  
        height="30"/>    
    </mx:ControlBar>   
  </mx:Panel>   
</mx:VDividedBox>  


功能就是当creat一个task后,那边tasklistbox.mxml能够list出来新的task,不知道为什么,create后,我加了调试信息TaskListBox.mxml中的listTasks()已经调用了,可是后台没有接收到那个get请求,然后handleTasksListResult函数,中得到的event是上次create时返回的那个event的信息,我在flex builder 3中加断点,以debug模式启动工程,那个send发的请求,后台就可以接收到,我后台用的rails,现在不知道为什么,是flex事件这边有问题,还是我rails那边有问题?
问题补充:
因为后台用的是rails,restful会生成一个路由,其中的/tasks.xml就是指向controller,task_controller的路由,如果是GET方法发送请求,就是到controller中的index方法,如果是用post方法发送请求,就是到controller中的create方法,在mxml中如果加入断点,就是rails那就可以收到这个请求,但是如果不加,rails就收不到这个请求,我是看mongrel的控制台信息中没有get请求,不知道是为什么?
     creationComplete="listTasks()">   是可以的,就是说当TaskListBox.mxml一生成的时候是可以list出所有tasks的,但是当我们生成一个新的task,这时应该响应<mx:Metadata>  
     [Event(name="taskCreate", type="com.pomodo.events.TaskEvent")]  事件,应该列出新的task,这时就不行了,问题就在这里。 加断点就可以,不知道是为什么?
  

Global site tag (gtag.js) - Google Analytics