Wednesday, August 8, 2012

Worklight's cross platform ability

Just to experiment with Worklight's cross platform ability I made a quick "Morse Code Translator" app to try on the different platforms.  Unfortunately I didn't have an actual Android Phone or a BlackBerry Phone to test my app on, but I was able to test it using the emulators.  Directions for how to set up the different emulators can be found on the Worklight tutorial site.  I would have tested my app on a iPhone as well, but I am working on a Windows computer.  By simply adding a new environment for each platform I wanted I was able to run my app on the emulators of a Windows Phone, Android Phone, and BlackBerry Phone.  No change in code was necessary for each platform.




Friday, August 3, 2012

Setting up an HTTP Adapter in Worklight

As one goes down the list of Worklight tutorials found on the Worklight website, the tutorials following the Worklight basics start to include Worklight adapters.  The definition given by the tutorial states "An adapter is a transport layer used by the Worklight® Platform to connect to various back-end systems."

Creating an adapter is simple.  Simply select "Worklight Adapter" from the Worklight drop down menu.  Eclipse will then ask you what project you want to create it in, what kind of adapter you would like to create (SQL, HTTP, or Cast Iron), and finally what you would like to name it.  Once the adapter is created there will be three files in the adapter folder: filtered.xls, adaptername.xml, and adaptername-impl.js.  Inside the javascript file there should be this code:

function getStories(interest) {
    path = getPath(interest);
    var input = {
        method : 'get',
        returnedContentType : 'xml',
        path : path
    };
    return WL.Server.invokeHttp(input);
}

function getStoriesFiltered(interest) {
    path = getPath(interest);
    var input = {
        method : 'get',
        returnedContentType : 'xml',
        path : path,
        transformation : {
            type : 'xslFile',
            xslFile : 'filtered.xsl'
        }
    };
    return WL.Server.invokeHttp(input);
}

function getPath(interest) {
    if (interest == undefined || interest == '') {
        interest = '';
    }

    else {
        interest = '_' + interest;
    }
    return 'rss/edition' + interest + '.rss';
}



To activate the adapter right click on the adapter and select "Run As > Deploy Worklight Adapter."  Now all that needs to be done is to call on the adapter from the app's JavaScript.  The tutorial from the Worklight website does an excellent job describing how to do so, so I won't waste time explaining something the tutorial can explain better.  The tutorial also comes with a sample program that retrieves a feed from Engadget for reference and experimenting.

Using a Windows Phone Environment I had one issue testing the sample program.  For an unknown reason the feed would not load in the app, even though I knew the adapter was working.  I was confident it was not the code since it was taken directly from the sample program.  After an extensive search for the issue a fellow IBMer found and pointed out the issue.  In the function "loadFeeds()" there is the code:

WL.Client.invokeProcedure(invocationData,{
    onSuccess : loadFeedsSuccess,
    onFailure : loadFeedsFailure,
});

The comma after loadFeedsFailure must be removed for the feed to load in a Windows Phone Environment.  I do not know if this is necessary for other environments.  (note: this issue has been fixed in an updated version of the sample code)  That is only the basic setup for a Worklight adapter, and from there one can learn and do much more with adapters.