Oct 08

When building Web based Flex applications, one of the common pitfalls is that the API you consume does not provide you with a crossdomain policy file and as a result, your Flex application cannot call directly to the application due to the Security Sandbox. (AIR applications does not have this Security issue)

One of the solution to this problem is using a proxy on your server, proxying the call to the API via your server. What the server does is to simply create a GET / POST request to the original endpoint and proxy back to the flex application all the results.

However, this solution will pose a problem in terms of scaling as all the request, originally meant to only goes to third-party API website, now actually goes through your server.

Enter JSONP

The preceding solution is one of the most common approach I used to use in most of my previous Mashup. However, after research AJAX, GWT and having a chat with my colleague from Seesmic, Matthew Eernisse, who is a Javascript and Ruby guru, I realize the power of JSONP and the simplicity of it working together with Flex to create mashup.

JSONP basically means JSON with Padding. From Wikipedia, it is defined as “JSONP or “JSON with padding” is a JSON extension wherein the name of a callback function is specified as an input argument of the call itself.

In the nutshell, it means you will have something like this:

<script src="http://books.google.com/books?jscmd=viewapi&bibkeys=ISBN9780596529857&callback=onresults">

with a function “onresults”

Together with Flex, you will have to use document.createElement(”script”) and then setAttribute to set the src. When the results are returned from the third-party endpoint, the specified function will be invoked. You can then parse the data, and simply put the data back to Flex.

JSONP, JQuery and External Interface

One of the usage of JSONP in a recent Flex application I have done is the TAC Preorder Form. The Preorder Form is a books ordering form for our sponsor Oreilly for The Actionscript Conference. Oreilly has provided us with a list of books’ ISBN number. Hoping that the users can actually browse the books cover and content first, I decide to pull in date from Google Books API, which provide information of the books’ thumbnail, as well as a preview page where the users can browse the books first few chapters first

The issue with the API is that there is no crossdomain policy file. Thus, the only way to call it is either using a server-side proxy or JSONP. I decide to go with JSONP for simplicity purpose, as well as to take load off the server.

Using JQuery, JSONP is extremely simple. The following codes shows an example of calling Google Books API.

$.getJSON("http://books.google.com/books?jscmd=viewapi&amp;bibkeys=ISBN"+isbn+"&amp;callback=?", function(data){
 
var bookInfo = data["ISBN" + isbn];
 
bookInfo.isbn = isbn;
 
//bookapp is a SWF with External Interface "getBookCallback" function
document.getElementById('bookapp').getBookCallback(bookInfo);
 
});

Essentially with JQuery, we can easily do away with the hassle of manually creating the javascript tag . We also need not to add codes to manually removing it upon completion. The build in “getJSON” method does it all. Upon completing the request, the callback function will be called, and we will pass the json object right back to Actionscript. Code as follows:

//constructor
public function PreorderClass()
{
 
super();
//initialize Cairngorm model
model = AppModelLocator.getInstance();
 
//create a getBookCallback function for Javascript to call this SWF
ExternalInterface.addCallback("getBookCallback", getBookCallback);
 
}
 
private function getBookCallback(e:*):void
{
 
/*
The Javascript object automatically becomes Actionscript Object with the following properties
bib_key
embeddable
info_url
preview
preview_url
thumbnail_url
isbn
*/
 
//using Cairngoem model and a custom vo "BookVO"
var book:BookVO = model.getBook(e.isbn);
book.thumbnail_url = e.thumbnail_url;
book.preview_url = e.preview_url;
 
}

Summary
Matthew pointed out to me a few shortcoming of JSONP. Firstly, there is no error handling. If the API endpoint is un-responsive, then you will not get any errors. The callback function will simply not be called at all. Thus, it is essential for developers to implement timeouts for their calls.

Secondly, if you are not using JQuery, the developer has to ensure that any codes that add a new javascript tag for JSONP, has to remove the javascript tag after the call has been completed.

In short, if you are creating a Web base Flex, JSONP provides you with a quick and easy way to call endpoint without crossdomain policy files, removing the hassle of setting up a server solution to proxy the request. Using JQuery, empower you to create JSONP calls within a few lines and then using External Interface to get the results back to the main Flex application.

Happy Mashing!

Jun 24

When we talk about creating presentation slides within the Browser, most people will think of SlideRocket, the Flex application that allows you to create professional slideshow without any installation.


The SlideRocket interface

Today, Mike Taylor bring to my attention this Javascript based application 280Slides 280Slides allows you to create your presentation online. Unlike Sliderocket, no login is required so the moment you go to the site, you are ready to go. Mac users will find 280Slides having strong resemblances to Keynote. A side to side comparison as below:

Keynote? 280Slides?

Keynote? 280 Slides?

Now which one is Keynote and which one is 280Slides? I let you figure that out yourself ;)

What I particularly like about 280Slides is that it allows you to bring in web videos and photos conveniently from its interface. For example, I can type in ’seesmic’ and 280Slides searches the videos of it from Youtube, and I can drag and drop them onto my slides for playback later. The ‘Present’ button convenient saves your slides settings (no login so far!) and now you have an ad-hoc slideshow with minimum effort. To save the slides however, you will now need to either register or enter your existing credentials.

A wall I hit will be that when I tried to download the presentation, it prompts me to download a file of pptx extension, something I do not have the luxury to open unless I have Powerpoint 2008. It is weird that while the founders behind 280Slides tries to imitate the user interface of Keynote, they provide us with a download file type that only someone with Powerpoint can open. Thanks to Ross from 280Slides for clarifying, the PPTX extension can actually be converted via a free tools on Apple, the Open XML File Format Convertor. I guess that will solve my problems then!

An option to download it as PDF will be great, even without any effects and the videos gone. Also, the ability to collaborate with other users will really value add to 280Slides too.

This is the slide show I have created from 280Slides in 10 minutes, dragging a couple of my existing vidoes on youtube and photos on facebook and flickr. Enjoy!


Video title:

Description: