Mapping client-side objects to Java objects
To represent a server-side Java object in a client application, you use the [RemoteClass(alias=" ")] metadata tag to create an ActionScript object that maps directly to the Java object. You specify the fully qualified class name of the Java class as the value of alias. This is the same technique that you use to map to Java objects when using RemoteObject components.
You can use the [RemoteClass] metadata tag without an alias if you do not map to a Java object on the server, but you do send back your object type from the server. Your ActionScript object is serialized to a Map object when it is sent to the server, but the object returned from the server to the clients is your original ActionScript type.
To create a managed association between client-side and server-side objects, you also use the [Managed] metadata tag or explicitly implement the mx.data.IManaged interface.
The following example shows an ActionScript class named samples.contact.Contact that has a managed association with a server-side Java class that is also named samples.contact.Contact:
package samples.contact
{
[Managed]
[RemoteClass(alias="samples.contact.Contact")]
public class Contact {
public var contactId:int;
public var firstName:String;
public var lastName:String;
public var address:String;
public var city:String;
public var state:String;
public var zip:String;
}
}
The following example shows the source code for the corresponding Java class on server:
package samples.contact;
public class Contact {
private int contactId;
private String firstName;
private String lastName;
...
public int getContactId() {
return contactId;
}
public void setContactId(int contactId) {
this.contactId = contactId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
...
}
The following example shows ActionScript code in an MXML file that uses the Contact object:
...
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.data.DataService;
import mx.data.messages.*;
import mx.data.events.*;
import mx.messaging.events.*;
import mx.rpc.*;
import mx.rpc.events.*;
import samples.contact.*;
[Bindable]
public var contacts:ArrayCollection;
[Bindable]
public var contact:Contact;
[Bindable]
private var ds:DataService;
public function initApp() {
contacts = new ArrayCollection();
ds = new DataService("contact");
...
ds.addEventListener(MessageEvent.RESULT, resultHandler);
ds.autoCommit = false;
ds.fill(contacts);
}
private function resultHandler(event:ResultEvent):void {
Hourglass.remove();
if (event.token.kind == "create") {
dg.selectedIndex = contacts.length - 1;
}
else if (event.token.kind == "delete" && contacts.length>0) {
var index:int = event.token.index
< contacts.length ? event.token.index : contacts.length -1;
dg.selectedIndex = index;
contact = contacts[index];
}
else if (event.token.kind == "fill" && contacts.length>0) {
dg.selectedIndex = 0;
contact = contacts[0];
}
}
private function faultHandler(event:DataServiceFaultEvent):void {
Hourglass.remove();
Alert.show(event.fault.faultstring, "Error");
if (event.item != null) {
ds.revertChanges(event.item as IManaged);
dg.selectedItem = event.item;
contact = event.item as Contact;
}
}
...
private function newContact():void {
dg.selectedIndex = -1;
contact = new Contact();
}
private function updateContact():void {
if (!contacts.contains(tmpContact))
contacts.addItem(tmpContact);
ds.commit();
}
private function deleteContact():void {
contacts.removeItemAt(dg.selectedIndex);
ds.commit();
}
]]>
</mx:Script>
...
http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001187.html
'ria > flex' 카테고리의 다른 글
| Background pattern - Flex (0) | 2008/02/27 |
|---|---|
| Flex Skin 사이트 (0) | 2008/02/21 |
| Mapping client-side objects to Java objects (0) | 2008/02/20 |
| Changing the close button skin on a Flex TitleWindow container (0) | 2008/02/15 |
| 플렉스와 엑셀 연동하기 (20) | 2008/02/15 |
| Flex Tip - ExternalInterface를 이용한 팝업 열기 (2) | 2008/02/13 |




댓글을 달아 주세요