Using AsyncToken class by RemoteObject or HTTPService

การใช้ RemoteObject หรือ HTTPService ติดต่อไปยัง Server เพื่อเอาข้อมูลมาใช้งาน ในบางครั้งเราอาจมีความจำเป็นบางอย่างที่ต้องการจำแนกแยกแยะว่าข้อมูลที่ได้มาด้วยเมธอด(method)หรือโอเปอเรชั่น(operation)นั้น เกิดขึ้นจากกรณีใด ในการแก้ปัญหานี้เราสามารถกำหนด Token ให้กับ RemoteObject เพื่อเป็นตัวช่วยในการตัดสินใจได้

(เคยเข้าใจผิดกับคำว่า Token กับ Totem ในหนังเรื่อง Inception ดีนะที่มีคนให้ความชัดเจนเรื่องนี้ไม่งั้นคงทำให้คนที่มาเรียนเข้าใจอะไรผิดๆ ไป)

Example how to use AsyncToken by RemoteObject


<?xml version="1.0" encoding="utf-8"?>

<!-- TokenExampleApp.mxml -->

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"

applicationComplete="initApp()">

<s:layout>

<s:VerticalLayout/>

</s:layout>

<fx:Script>

<![CDATA[

import mx.collections.ArrayCollection;

import mx.messaging.messages.RemotingMessage;

import mx.rpc.AsyncToken;

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

[Bindable] private var ac:ArrayCollection;

private function initApp():void{

ac = new ArrayCollection(

[

{datetime:"-"},

{datetime:"-"},

{datetime:"-"},

{datetime:"-"},

{datetime:"-"}

]

);

}

protected function bt_clickHandler(event:MouseEvent):void

{

var token:AsyncToken = ro.getOperation('getDateTime').send( dg.selectedItem );

token.selectedIndex = dg.selectedIndex; // Set token property and value here

}

protected function ro_faultHandler(event:FaultEvent):void

{

}

protected function ro_resultHandler(event:ResultEvent):void

{

var rMsg:RemotingMessage = event.token.message as RemotingMessage;

switch( rMsg.operation ){

case 'getDateTime':

ac.setItemAt( event.result, event.token.selectedIndex ); //Check token property value here

break;

}

}

]]>

</fx:Script>

<fx:Declarations>

<!-- Place non-visual elements (e.g., services, value objects) here -->

<s:RemoteObject id="ro" destination="zendamf" source="TokenExService"

fault="ro_faultHandler(event)"

result="ro_resultHandler(event)"/>

</fx:Declarations>

<s:Button id="bt" label="Send" enabled="{dg.selectedIndex + 1}" click="bt_clickHandler(event)"/>

<mx:DataGrid id="dg" dataProvider="{ac}">

<mx:columns>

<mx:DataGridColumn dataField="datetime" width="200"/>

</mx:columns>

</mx:DataGrid>

</s:Application>


<?php

/**
 * Description of TokenExService
 *
 * @author noppadonsodram
 */

class TokenExService {
    //put your code here

    function getDateTime($vo){
        $vo->datetime = date("F j, Y, g:i:s a");
        return $vo;
    }

}

หัวข้อเกี่ยวกับการใช้ ​RemoteObject หรือ HTTPService

http://thaiflexdev.wordpress.com/2009/10/04/introduction-to-zendamf/

http://thaiflexdev.wordpress.com/2009/07/11/amfphp-beginnign-level/

http://thaiflexdev.wordpress.com/2010/07/15/php-variable-value-when-send-by-httpservice-or-remoteobject-2/

http://thaiflexdev.wordpress.com/2010/07/15/send-array-to-flex-app/

Advertisement

About knopsod
Developer by Flex/Flash, ActionScript, PHP, Java, MySQL,PostgreSQL, Oracle, Ebay, PayPal, Alibaba, iOffer รับปรึกษา สอน และพัฒนาซอฟแวร์ ซื้อ ขาย ประมูลสินค้าออนไลน์

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.