2. Basic Remoting
Context & Remote Components
The Context object is the central part of Tide. It is the client-side representation of the server contexts.
It is stored in the Tide singleton instance and is thus accessible from anywhere in the Flex application. There are subclasses of the Tide class depending on the server technology (Seam, Spring, EJB).
import org.granite.tide.seam.Context;
var tideContext:Context = Seam.getInstance().getSeamContext();
import org.granite.tide.spring.Context;
var tideContext:Context = Spring.getInstance().getSpringContext();
import org.granite.tide.ejb.Context;
var tideContext:Context = Ejb.getInstance().getEjbContext();
By default, the Tide instance is wired to a destination named seam, spring, or ejb. Optionally, it is possible to initialize it with another destination with getInstance("mydestination"). Only the very first call to getInstance("myDestination") will decide the destination. All following calls to getInstance() will use the previously defined destination whatever argument is passed.
The server components can then be referenced by their name as properties of the Context. For example, getting tideContext.helloWorld returns a proxy to a server component named helloWorld.
To call a remote method, you can call any method on this proxy object and it will be processed as a remote call and sent to the server with its parameters.
tideContext.helloWorld.sayHello("Jimi", helloResult, helloFault);
The helloResult and helloFault are both optional and define response handlers for success and fault cases.
The previous line will result in a remote call on the method sayHello of the helloWorld component with the String parameter "Jimi":
private function helloResult(event:TideResultEvent):void { Alert.show(event.result); }
The method result is then provided in the result property of the TideResultEvent.
Of course, all objects that can be serialized by GDS are supported, even lazy loaded objects or collections.
Errors can be handled by providing a faultHandler:
private function helloFault(event:TideFaultEvent):void { Alert.show(event.fault.toString()); }
The Fault object represents the remote exception thrown on the server.
Property accessors on the client proxy are not translated to remote calls. If you need to call a remote getter, you can just do (with the parenthesis notation):
tideContext.myComponent.getMyProperty(myPropertyResult);
but not
tideContext.myComponent.myProperty = "value";
var value:Object = tideContext.myComponent.myProperty;
This second option will not trigger remote calls, rather just set and get the current local value of myProperty. In the case of Seam, values set locally on the component are synchronized with the server component on the next remote call. For Spring and Ejb, this will have absolutely no effect.
Using the ITideResponder Interface
In some cases, you may need to pass some value to the result/fault handler to be able to distinguish different calls on the same method. You can then implement the ITideResponder interface or use the default TideResponder implementation that can hold a token object:
public function call():void { var responder1:TideResponder = new TideResponder(helloResult, helloFault, "firstCall"); var responder2:TideResponder = new TideResponder(helloResult, helloFault, "secondCall"); tideContext.helloWorld.sayHello("Jimi", responder1); tideContext.helloWorld.sayHello("Jimi", responder2); } private function helloResult(event:TideResultEvent, token:Object):void { if (token == "firstCall") Alert.show(event.result); }
In this case, the Alert will show up only once for the first call.
