Building RIAs on J2EE Foundations 
by Joe Rinehart

Listing One

package com.firemoss.tasks.model
{
    import mx.utils.UIDUtil;
     // Alias'd to server-side class / component
      [RemoteClass(alias="com.firemoss.tasks.model.Task")]
    // Bindable makes this class a subject for data binding
      [Bindable]
    /** Model of a Task. */
    public class Task
    {
        /** Unique Id. */
        public var id:String = UIDUtil.createUID();
        /** Name of the task. */
        public var task:String;
        /** Is the task complete? */
        public var complete:Boolean;
    }
}


Listing Two

<!-- MODEL -->
<!-- Master list of all tasks -->
<mx:ArrayCollection id="tasks" />
<!-- Filtered list of completed tasks -->
<mx:ListCollectionView 
    id="incompleteTasks" 
    filterFunction="filterIncompleteTask" 
    list="{tasks}"/>

Listing Three

<!-- List of tasks -->
<mx:List
    id="taskList"
    height="100%" width="100%"
    labelField="task"
    dataProvider="{showCompletedTasks.selected ? tasks : incompleteTasks}"
    change="taskListChangeHandler(event)">
    <mx:itemRenderer>
        <mx:Component>
            <view:TaskItemRenderer />
        </mx:Component>
    </mx:itemRenderer>
</mx:List>


Listing Four

<!-- BUSINESS SERVICES -->
<!-- Subscribes to realtime messages from the application server -->
<mx:Consumer 
    id="consumer" 
    destination="ColdFusionGateway" 
    message="taskMessageHandler(event)" />
<!-- Allows RPC-style invocations of server-side service methods -->
<mx:RemoteObject 
    id="taskService" 
    destination="ColdFusion" 
    source="com.firemoss.tasks.service.TaskService" >
    <mx:method name="listTasks" result="listTasksResultHandler(event)" />
    <mx:method name="saveTask" />
</mx:RemoteObject>


Listing Five

/** Invoked when the server completes an operation of taskService */
private function listTasksResultHandler(event:ResultEvent):void
{
    // Server returns an Array of Task instances: update our model.
    this.tasks.source = event.result as Array;
}

Listing Six

<cfcomponent output="false" hint="DTO for a Task" alias="com.firemoss.tasks.model.Task">
<cfproperty name="id" type="string" hint="UUID of this task." />
<cfproperty name="task" type="string" hint="Name of this task, e.g., ""Buy Milk""" />
<cfproperty name="complete" type="boolean" hint="Is this task complete?" />
<!--- Initialize --->
<cfset this.id = "" />
<cfset this.task = "" />
<cfset this.complete = false />
</cfcomponent>

Listing Seven

<!--- Notify client applications --->
<cfset taskMessage = structNew() />
<!--- Flex messaging destination to use: Matches <mx:Consumer /> tag attribute --->
<cfset taskMessage.destination = "ColdFusionGateway" />
<cfset taskMessage.body = arguments.task />
<cfset sendGatewayMessage("taskGateway", taskMessage) />



2


