This is my mxml file:
<?xml version="1.0" encoding="utf-8"?>
<!--
////////////////////////////////////////////////////////////////////////////////
// ADOBE SYSTEMS INCORPORATED
// Copyright 2007 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
// terms of the Adobe license agreement accompanying it. If you have received this file from a
// source other than Adobe, then your use, modification, or distribution of it requires the prior
// written permission of Adobe.
////////////////////////////////////////////////////////////////////////////////
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
backgroundAlpha="0" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
include "EmployeesCRUD.as";
]]>
</mx:Script>
<!-- [various URLs for HTTPService below]
url="http://examples.adobe.com/flex3/workingwithdata/employees.php"
url="http://www.tipsandapps.com/CRUD/employees.asp"
url="http://localhost:8100/flex3samples-war/workingwithdata/employees.jsp"
url="http://examples.adobe.com/flex3app/flex3samples/workingwithdata/employees.jsp"
Local CF server:
url="http://localhost:8501/FxGSE/employees.cfc"
-->
<mx:HTTPService
id="employeesService"
url="http://localhost:8501/employees.cfc"
resultFormat="e4x"
useProxy="false"/>
<mx:ViewStack id="viewstack1" width="100%" height="100%" >
<mx:Canvas label="Form View" width="100%" height="100%">
<mx:Form horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF">
<mx:FormItem label="Query Employees ">
<mx:Button label="Submit" click="fill()" width="100"/>
</mx:FormItem>
</mx:Form>
</mx:Canvas>
<mx:Panel label="DataGrid View" width="100%" height="100%">
<mx:DataGrid width="100%" height="100%" dataProvider="{listData}">
<mx:columns>
<mx:DataGridColumn dataField="firstName" headerText="First Name"/>
<mx:DataGridColumn dataField="lastName" headerText="Last Name"/>
<mx:DataGridColumn dataField="officePhone" headerText="Phone"/>
</mx:columns>
</mx:DataGrid>
<mx:Form backgroundColor="#FFFFFF">
<mx:FormItem label="Add New Employee">
<mx:Button label="Add..." click="{viewstack1.selectedIndex = 2}" width="100"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Canvas label="Add New Employee View" width="100%" height="100%">
<mx:Form horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF">
<mx:FormItem label="First Name">
<mx:TextInput id="inputFirst"/>
</mx:FormItem>
<mx:FormItem label="Last Name">
<mx:TextInput id="inputLast"/>
</mx:FormItem>
<mx:FormItem label="Phone">
<mx:TextInput id="inputPhone"/>
</mx:FormItem>
<mx:FormItem label="Add Employee ">
<mx:Button label="Add" click="insertEmployee()" />
</mx:FormItem>
</mx:Form>
</mx:Canvas>
</mx:ViewStack>
</mx:Application>
This is my .as file:
/*
////////////////////////////////////////////////////////////////////////////////
// ADOBE SYSTEMS INCORPORATED
// Copyright 2007 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
// terms of the Adobe license agreement accompanying it. If you have received this file from a
// source other than Adobe, then your use, modification, or distribution of it requires the prior
// written permission of Adobe.
////////////////////////////////////////////////////////////////////////////////
*/
import mx.rpc.events.ResultEvent;
import mx.collections.XMLListCollection;
private var params:Object = new Object();
[Bindable]
private var listData:XMLListCollection;
public function resultHandler(event:ResultEvent):void {
var result:XML = XML(event.result);
var xmlList:XMLList = result.data.children();
listData = new XMLListCollection(xmlList);
}
public function insertItemHandler(event:ResultEvent):void {
fill();
}
public function fill():void{
employeesService.removeEventListener(ResultEvent.RESULT,insertItemHandler);
employeesService.addEventListener(ResultEvent.RESULT,resultHandler);
employeesService.method = "GET";
params['method'] = "FindAll";
employeesService.cancel();
employeesService.send(params);
viewstack1.selectedIndex=1;
}
public function insertEmployee():void{
employeesService.removeEventListener(ResultEvent.RESULT,resultHandler);
employeesService.addEventListener(ResultEvent.RESULT,insertItemHandler);
employeesService.method = "POST";
params = {"method": "InsertEmployee", "id": NaN, "firstName": inputFirst.text,
"lastName": inputLast.text, "officePhone": inputPhone.text};
employeesService.cancel();
employeesService.send(params);
clearInputFields();
}
private function clearInputFields():void{
inputFirst.text = "";
inputLast.text = "";
inputPhone.text = "";
}
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="ro.getData()" viewSourceURL="srcview/index.html">
<mx:RemoteObject id="ro" destination="ColdFusion" showBusyCursor="true" source="flex.employees">
<mx:method name="getData" result="getDataResult(event)" />
<mx:method name="saveNewData" result="getDataResult(event)" />
<mx:method name="saveOldData" result="getDataResult(event)" />
<mx:method name="deleteData" result="getDataResult(event)" />
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
private var isNew:Boolean = true;
public function getDataResult(e:ResultEvent):void{
dg.dataProvider = e.result as ArrayCollection;
makeNew();
}
private function makeNew():void{
aName.text = "";
type.text = "";
isNew = true;
}
private function save():void{
if(isNew){
ro.saveNewData(aName.text,type.text);
}else{
ro.saveOldData(aName.text,type.text,dg.selectedItem.id);
}
}
private function deleteData():void{
if(dg.selectedIndex > -1 )ro.deleteData(dg.selectedItem.id);
}
]]>
</mx:Script>
<mx:DataGrid id="dg" change="isNew = false" width="366">
</mx:DataGrid>
<mx:Panel width="372" height="146" layout="absolute">
<mx:Form top="0" bottom="0" right="0" left="0">
<mx:FormItem label="Name">
<mx:TextInput id="aName" text="{dg.selectedItem.name}"/>
</mx:FormItem>
<mx:FormItem label="Type">
<mx:TextInput id="type" text="{dg.selectedItem.type}"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
<mx:Button label="New" click="makeNew()"/>
<mx:Button label="Save" click="save()" />
<mx:Button label="Delete" click="deleteData()" />
</mx:Application>
<cfcomponent output="false">
<cfset mstest = "media">
<cffunction name="getData" access="remote" returntype="Query">
<cfset var local = {} />
<cfquery name="local.q" datasource="#mstest#">
select id, name, type
from test
</cfquery>
<cfreturn local.q/>
</cffunction>
<cffunction name="saveNewData" access="remote" returntype="Query">
<cfargument name="name" type="string" required="true"/>
<cfargument name="type" type="string" required="true"/>
<cfquery datasource="#mstest#">
insert
into test
( name, type )
values ( <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.name#"/>,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.type#"/> )
</cfquery>
<cfreturn getData() />
</cffunction>
<cffunction name="saveOldData" access="remote" returntype="Query">
<cfargument name="name" type="string" required="true" />
<cfargument name="type" type="string" required="true" />
<cfargument name="id" type="Numeric" required="true" />
<cfquery datasource="#mstest#">
update test
set name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.name#" />,
type = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.type#" />
where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.id#" />
</cfquery>
<cfreturn getData() />
</cffunction>
<cffunction name="deleteData" access="remote" returntype="Query">
<cfargument name="id" type="string" required="true">
<cfquery datasource="#mstest#">
delete
from test
where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.id#" />
</cfquery>
<cfreturn getData() />
</cffunction>
</cfcomponent>
mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="dataBaseRequest.myDataBaseFunction();" viewSourceURL="srcview/index.html">
<!--=====The Source for RemoteObject needs to be from the wwwroot Folder
I have my WorkSpace under the wwwroot Folder
so you have to start from after the wwwroot folder and work
down until you hit the cfc file
-->
<mx:RemoteObject destination="ColdFusion" id="dataBaseRequest"
source="flex.cfc.DataBaseCFC">
<mx:method name="myDataBaseFunction" result="onResultService(event)"
fault="onFaultService(event)"/>
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
/**
* @Private
* Since we are returning a query from the DataBase in ColdFusion
* and we are using remoting then Flex will recieve the Query as
* an ArrayCollection
*/
[Bindable]
private var remoteCollection:ArrayCollection;
/**
* @private
* event.result will be where the data is stored
*/
private function onResultService(event:ResultEvent):void
{
remoteCollection=event.result as ArrayCollection;
}
private function onFaultService(event:FaultEvent):void
{
mx.controls.Alert.show(event.fault.toString());
}
]]>
</mx:Script>
<mx:DataGrid id="RemotedataGrid" dataProvider="{remoteCollection}"
width="500" height="500"/>
</mx:Application>
cfc:
<cfcomponent output="no">
<cffunction name="myDataBaseFunction" access="remote" returntype="query">
<cfquery datasource="media" name="simpleQuery" >
S firstName, lastName
F employees
</cfquery>
<cfreturn simpleQuery>
</cffunction>
</cfcomponent>
Videos, tutorials and code examples.. :)