The Flex SerializationFilter Class

Posted in Flex on January 15th, 2010 by admin – 1 Comment

The other day while digging through the API docs for the HTTPService class, I noticed the addition of a new property called serializationFilter. This piqued my interest. I clicked the property name to get the more detailed explanation. Here’s what it says:

Provides an adapter which controls the process of converting the HTTP response body into ActionScript objects and/or turning the parameters or body into the contentType, URL, and and post body of the HTTP request. This can also be set indirectly by setting the resultFormat by registering a SerializationFilter using the static method: SerializationFilter.registerFilterForResultFormat(“formatName”, filter)

If you can’t already see how this could be useful let me give you an example. Have you ever had to work with a service that returned JSON? You probably did something like this.

<mx:HTTPService url="/path/to/json/data" result="resultHandler(event);" />

<mx:Script>
 <![CDATA[
     import com.adobe.serialization.json.JSON;
     import mx.rpc.events.ResultEvent;

     private function resutlHandler(event:ResultEvent):void
     {
         var result:Object = JSON.decode(event.result as String);
         // Do something with the result
     }
 ]]>
</mx:Script>

This method is fine and it works well for a single service. But what if you have multiple JSON services? You’ll end up having to decode the result string in every additional result handler you have. Not a big deal but wouldn’t it be nice if the HTTPService could deserialize the results for us like it already does for XML? This is where the SerializationFilter comes in. By subclassing the SerializationFilter and overriding the deserializeResult method we can encapsulate the JSON decoding process and free ourselves from the repetitive task. Here’s a simple JSONSerializationFilter class.

package org.tylerchesley.serialization
{
 import com.adobe.serialization.json.JSON;

 import mx.rpc.http.AbstractOperation;
 import mx.rpc.http.SerializationFilter;

 public class JSONSerializationFilter extends SerializationFilter
 {

     public function JSONSerializationFilter()
     {
         super();
     }

 /* Overriden Methods */

     override public function deserializeResult(operation:AbstractOperation, result:Object):Object
     {
         return JSON.decode(result as String);
     }

 }
}

There’s two ways to use a custom SerializationFilter class. You can set the serializationFilter property of your service with an instance of your custom
SerializationFilter. For example:

<mx:HTTPService url="/path/to/some/json/data">
    <mx:serializationFilter>
        <tc:JSONSerializationFilter />
    </mx:serializationFilter>
</mx:HTTPService />

Or you can use the static registerFilterForResultFormat method like this.

<mx:Script>
 <![CDATA[
    import org.tylerchesley.serialization.JSONSerializationFilter;
    import mx.rpc.http.SerializationFilter;

    private function init():void
    {
        SerializationFilter.registerFilterForResultFormat("application/json", new JSONSerializationFilter());
    }

 ]]>
</mx:Script>

By using the latter method any HTTPService object with the content type set to “application/json” will automatically use our new JSONSerializationFilter.

Aloha on Rails

Posted in Conference, Rails, Ruby on August 10th, 2009 by admin – Be the first to comment

Just wanted to let everyone know about Aloha on Rails Hawaii Ruby On Rails Conference – Waikiki, Oahu, Hawaii – October 4-6 2009. My friend, Seth Ladd, has done a great job organizing this conference. Even if you don’t program in Ruby or use Rails I think you will find value in this conference. It’s probably the best bang for your buck, especially if you snag the early bird rate. Plus it’s a great excuse to come to Hawaii. Check it out! http://www.alohaonrails.com/
.

Bark! Growl Like Notifications for Flex

Posted in Components, Flex on August 8th, 2009 by admin – 17 Comments

* I’ve updated the source code for the new Flex 4 beta 2. I’ve also fixed the null pointer error. You can download it here or checkout the latest from the Github.

Bark Logo

Introducing Bark! a notification framework for Flex modeled after the uber-awesome Growl framework for the Mac.

Bark in action.

Bark in action.

Recently, I’ve worked on several projects where I needed a Growl like notification to alert users of various events happening in the background. I did a Google search for Flex Growl and came across several solutions for Air and Growl but not many results for a web-based solution. I did come across Francis Lukesh’s Rawr! component. While I liked the result and thought it was well-thought out and implemented, I wanted a more general framework and lessĀ  dependencies on any non-Flex framework code (Rawr! was written using the HydraFramework and uses Degrafa for skinning). Originally, I intended to write the framework using the Flex 3 SDK, but I had been playing around with the new Flex 4 beta and was very impressed with the ease of skinning components. I thought here was a perfect opportunity to get my feet wet with Flex 4. I know I’ve possibly condemned myself to rewriting code as the SDK evolves from beta to production but I thought it was worth it for the improved functionality and ease of use. I might consider writing a Flex 3 version if there is a enough demand. You can check out a demo here (view source is enabled) and download the source code here. You can also checkout the source code from github here. Any comments or suggestions welcome.

Features:

  • Utilizes the new Flex 4 Beta SDK for improved skinning ability and other improvements.
  • Notifications are completely configurable. The title, description, duration, icon, and renderer can all be configured using the Notification object.
  • Multiple skins and styles can be setup using the type property of the Notification object. If the type property is set it will be used as the styleName for the renderer. This makes it easy to have multiple skins for a specific renderer (see the demo for an example of this).
  • Custom renderers can easily be specified using the renderer property on the Notification object.
  • Notification events can be dispatched anywhere in the display list to trigger a notification display. Once the NotificationManager is initialized it will listen for all notification events that bubble up the display list (by default it listens for events on the top level application but you can also pass in a custom display object to listen to).
  • Callback in place for notification renderer clicks. Simply listen for a NotificationEvent.notificationItemClick event on the notification object. * Note – calling preventDefault() on the event will stop the renderer from being removedĀ  from the display list.
  • Hide and show effects for the notifications renderers are customizable globally using the NotificationManager.

To Do:

  • Switch notification renderer layout to use the new Flex 4 layouts for more flexibility and configuration options.
  • Maybe implement Flex 3 version depending on demand.
  • Documentation
  • More examples

Pan/Extent Control for ArcGIS Flex API with history management

Posted in Uncategorized on April 26th, 2009 by admin – 1 Comment

If you’ve ever used the ArcGIS API to create maps for your Flex apps, you’ve undoubtedly noticed the lack of a pan or extent control. I know I have. I was so envious of the Google maps guys with their fancy, smancy, circle thing in the top, left corner that I decided to remedy the situation myself. Hell, I think I might have even out done them. At least, I’ve never seen a Google map control that lets you step forwards and backwards through your navigation history. Eat my shorts Google! You can view an sample application here. View source is enabled on the demo but it looks like you have to right click on the toggle button bar in the top, right corner because the ESRI controls provide their own context menus. You can download the source here.

Eat my shorts Google!

Eat my shorts Google!