Archive for the ‘MXML’ tag
Route Framework: Guards and Execution Control
Most instances of IAction also implement IGuarded interface, that allows programmer to control when certain actions will be executed. It acts a lot like guards from declarative languages, such as Prolog, Erlang or Scala. Essentially speaking: if IGuard.when evaluates into true IAction will be executed successfully, otherwise it won’t.
Predicate for guard ( value of IGuard.when ) can be in a form of: Boolean, function ():Boolean or function ( percept:Object ):Boolean. Here is small example with fluent dsl:
var router:IRouter = new Router().
route( Handle, { event: LoginEvent.LOGIN } ).
run( Run, { closure: Alert.show, arguments: ["Logging in "] } ).
when( check ).
end;
protected function check( percept:LoginEvent ):Boolean
{
if( percept.username == null || percept.username.length == 0 ||
percept.password == null || percept.password.length == 0 )
return false;
return true;
}
Action Run will be executed only if incoming LoginEvent has defined username and password. Of course, you will prefer sophisticated validation than simple length check.
Following example shows how to use multiple IGuarded.when predicates with some Functional Programming mojo:
var router:IRouter = new Router().
route( Handle, { event: LoginEvent.LOGIN } ).
run( Run, { closure: Alert.show, arguments: ["Logging in "] } ).
when( check( "username" ) ).
when( check( "password" ) ).
end;
/**
* Checks if <code>String</code> that's evaluated via <code>percept[property]</code> is not empty and is not <code>null</code>.
*
* @param property <code>String</code> name of property on <code>percept</code> argument.
* @param percept <code>percept</code> received by <code>IRoute</code> for testing.
*
* @returns <code>Boolean</code>. <code>true</code> if string is not <code>null</code> and contains at least 1 symbol, <code>false</code>otherwise</code>.
*/
protected function checkString( property:String, percept:LoginEvent ):Boolean
{
var string:String = percept[property];
if( string == null || string.length == 0 )
return false;
return true;
}
/**
* Higher order function. Simplistic Curry function.
*/
protected function check( property:String ):Function
{
return function( percept:LoginEvent ):Boolean
{
return checkString( property, percept );
}
}
I’ve used Higher Order Function, to construct actual predicates. If you have some experience working with Functional Programming, you will find this methods more elegant and logical.
MXML syntax also allows us to use Data Bindings in this example:
<route:Router id="router">
<route:Handle event="{ LoginEvent.LOGIN }">
<route:Run closure="{ Alert.show }" arguments="{ ['Logging in'] }"
when="{ router.percept.username != null && router.percept.username.length > 0 &&
router.percept.password != null && router.percept.password.length > 0 }" />
</route:Handle>
</route:Router>
No additional functions are requited.
This is rather simple, yet very powerful mechanism of controlling execution cycle of application, without need to create special actions that will check data before execution that can be achieved by simple injection of a guard.
Futher Reading
RESTful (Prototyping) Framework
That was a long long time since I wrote something about Flex, But looks like I found some time for both Game and RIA industries.
I want to share some of my RESTful client library ideas and concepts. I’m trying to build something very laconic, light and still powerful to connect to various REST Services.
Please note, current version is PREVIEW, which means far before ALPHA, I’m just trying around some stuff, can be not pretty.
So, here is how I want this stuff to work:
Let’s say we have imaginary Social Network, with following REST Operations:
- http://api.socnet.com/users/search
- http://api.socnet.com/users/show
- http://api.socnet.com/users/friends/list
- http://api.socnet.com/photos/new_ones
I can build (IMO) pretty Service structure right in MXML:
<kii:Service id="main" endPoint="http://api.socnet.com/"> <kii:Service id="users" endPoint="users"> <kii:Service id="friends" endPoint="friends"/> </kii:Service> <kii:Service id="photos" endPoint="photos"/> </kii:Service>
And then access it simly like this:
users.post('show', {some_param: someParam});
fiends.get('list');
photos.get('new_ones', {limit: 25});
Later on I can extend Service classes to include concrete methods like Users.list() and Users.update(); but framework is ready to use and might be much more useful than HTTPService right out of box. Again I’m not sure whether I will be using this framework for prototypes only or for release projects too.
You can get source code and binaries from Google Code