Archive for the ‘opensource’ tag
Route Framework: Interfacing
[ToDo: add link to Route Framework introduction]
[ToDo: add link to overview of the Route]
There is 3 basic ways of working with Route Framework: ActionSctipt, ActionScript-Fluent and MXML.
ActionScript: Allows you to work with Route Framework on a low level. It should be used in case you are interested in generating FrontControllers in Runtime from Configuration files or Data Base settings.
Fluent: This is also a way of defining Routers in ActionScript, but much less verbose, and easier to read and define. em>You can read more about fluent programming on Martin Fowler’s Bliki.
MXML: This style allows programmer to define Routers in MXML.
Let’s get some abstract but nevertheless good example: We’ll create two TextAreas original and copy and link their values via Model which we call LabModel. Overral data flow can be represented as:
original.text → labModel.text → copy.text
ActionScript: Low Level
// Create a pattern that will match input to flash.events.Event: var eventPattern:IPattern = new Pattern; eventPattern.matcher = eu.kiichigo.route.pattern.match.type; eventPattern.store( "type", flash.events.Event ); // Create Pattern that will match "bindPlease" string. var bindPleasePattern:IPattern = new Pattern; bindPleasePattern.matcher = eu.kiichigo.route.pattern.match.values; bindPleasePattern.store( "type", "bindPlease" ); // This action will bind view to model var view2model:Bind = new Bind; view2model.from = original; view2model.to = LabManager; view2model.text = "text"; // And this will bind model to view. var model2view:Bind = new Bind; model2view.from = LabManager; model2view.to = copy; model2view.text = "text"; // Create Route that will be catching "bindPlease" events: var routeBindPlease:IRoute = new Route; routeBindPlease.pattern = new Patterns( eventPattern, bindPleasePattern ); routeBindPlease.add( view2model ); routeBindPlease.add( model2view ); routeBindPlease.sensor = Events; var router:eu.kiichigo.route.kore.IRouter = new eu.kiichigo.route.kore.Router; router.add( routeBindPlease );
Here is what happens:
- We define two patterns:
- First will match incoming
perceptsand filter by Class:percept is flash.events.Event - Second pattern checks value:
percept.type = "bindPlease"
- First will match incoming
Bind actions and set them up.Fluent and MXML
Following two examples show how same logic can be defined in Fluent and MXML DSLs:
var router:IRouter = new Router().
route( Handle, { event: "bindPlease" } ).
action( Bind, { from: original, to: LabManager, text: "text" } ).
action( Bind, { from: LabManager, to: copy, text: "text" } ).
end;
<route:Router>
<route:Handle event="{ 'bindPlease' }">
<route:Bind from="{ original }" to="{ LabManager }" text="text" />
<route:Bind from="{ LabManager }" to="{ copy }" text="text" />
</route:Handle>
</route:Router>
I want to stress attention on Handle. Handle is one of the pre-defined types of Route that handles percepts of flash.events.Event. There is other Route types available:
ValueObject: Will perceive and process data from RPC services.
Inject: Will perceive and process created by Router instances, or added DisplayObjects to DisplayList.
Programmer can also create own Routes and Sensors to work with other ways of messaging, such as custom implementation of Observer or Delegating Event Model design patterns.