ActionScript オブジェクトアダプタの使用

ActionScript オブジェクトアダプタは、一時データのみを必要とし、バックエンドのデータリソースを必要とせず、Flex クライアントアプリケーションがデータオブジェクトのただ 1 つの作成者および使用者であるシナリオでの使用を目的としています。Data Management Service は、ActionScript オブジェクトアダプタを使用してメモリ内のオブジェクトを管理します。ActionScript オブジェクト内の任意のフィールドを使用し、宛先のメタデータセクションの identity エレメントに一意の identity を提供できます。組み合わせた identity を設定する場合は、フィールドを複数指定します。identity を指定しない場合は、デフォルトで ActionScript オブジェクトの uid によってオブジェクトの identity が提供されます。

次の表では、ActionScript オブジェクトアダプタに固有の宛先エレメントについてそれぞれ説明します。これらのエレメントは server エレメントの子エレメントです。

エレメント

説明

<create-security-constraint ref="sample-users"/>

ref 属性で参照されるセキュリティ制限を、create 要求に適用します。

<read-security-constraint ref="sample-users"/>

ref 属性で参照されるセキュリティ制限を、fill 要求に適用します。

<update-security-constraint ref="sample-users"/>

ref 属性で参照されるセキュリティ制限を、update 要求に適用します。

<delete-security-constraint ref="sample-users"/>

ref 属性で参照されるセキュリティ制限を、delete 要求に適用します。

次の例のクライアントアプリケーションは、ActionScript アダプタを使用し、すべてのクライアントにわたって単一のデータアイテムがまだ TextArea コントロールに存在しない場合に、そのデータアイテムを永続化します。バックエンドのデータリソースが存在しないため、サーバーが停止した場合はデータが失われます。

<?xml version="1.0"?>
<!-- fds\datamanagement\ASAdapter.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 height="100%" width="100%"
 creationComplete="initApp();">

    <mx:Script>
        <![CDATA[
            import mx.data.DataService;
            import mx.rpc.AsyncToken;

            public var noteObj:Object = new Object();
            [Bindable]
            public var getToken:AsyncToken;
            private var ds:DataService;

            private function initApp():void {
            ds = new DataService("notes");
            ds.autoCommit = false;
            noteObj.noteId = 1;
            noteObj.noteText =
                "Type your notes here and share them with other clients!";
            getToken = ds.getItem(noteObj, noteObj);
            }
      ]]>
    </mx:Script>
    <mx:Binding source="log.text" destination="getToken.result.noteText"/>
    <mx:TextArea id="log" width="100%" height="100%" text="{getToken.result.noteText}"/>
    <mx:Button label="Send" click="ds.commit();"/>
</mx:Application>

サーバーサイドの設定は、"data-management_config.xml" ファイルにある次の宛先のみです。

<destination id="notes">
    <adapter ref="actionscript"/>
        <properties>
        <metadata>
            <identity property="noteId"/> 
        </metadata>
        </properties>
</destination>

アイテムにはそれぞれ一意の ID を持たせる必要があります。ID を自動的に作成するバックエンドのデータリソースはありません。したがって、ArrayCollection のデータアイテムを必要とするアプリケーションでは、管理されたオブジェクトの作成時にクライアントでアイテム ID を生成する必要があります。次の例のコードでは、Math.random() メソッドを使用して customer.custId を生成しています。実際のアプリケーションでは、custId は設定ファイル内の宛先で識別子として指定するプロパティになります。

        private function newCustomer():void         {
            dg.selectedIndex = -1;
            customer = new Customer();
            customer.custId = Math.random() * 1000;
        }

Flex 2.01