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.

This is a common problem with javascript and the browser error handling of stray commas and is usually hard to find the root cause. Worklight has been notified of the bug and that it should be fixed regardless of whether it is seen on other browsers/platforms. Great write up!
ReplyDelete