Accessing news feeds with IBM Worklight – Part 2
In my previous post I looked at the way adapters get news from RSS feeds. But the adapter is not interested in the news! All it wants to do is send it back to you. So you need to call the adapter from an application. This blog post looks at how to combine application and adapter.
What the application needs to know
Here is a diagram that sums up what is happening on the device. The application (which is of course on the device) needs to know where the server is, which adapter to invoke, which function to call on the adapter and what data to pass with the invocation.
Let's start with the server address. This is set in the application descriptor, in a tag with the explicit (though long!) name work
<wor
Next, open the main JavaScript file of your application, the one called NewsApp.js. Add a function to call the adapter:
function callAdapter (theData) { }
The adapter is invoked by calling WL.C
Add this to the function:
var invocationData = {
adap
var options =
{onSuccess: processResponse, onFailure: processResponse };
WL.C
Since you have given a function name in options, you need to create it! Here is one that will display the first result title on a console:
function processResponse (result) {
if (res
alert(
"Result: "+re
else
alert("Feed was not retrieved correctly");
}
In order to invoke callAdapter() you can add a line of code to the init function:
function wlCommonInit () { call
Try running this! Since it is an application you cannot simulate the run in Worklight Studio. Open a browser to localhost on port 8080 and click “common resources.” After a moment, you will see the title of the first retrieved feed. (At least if you have no problem you will see it. Otherwise: coding is a game of snakes and ladders. Go back to square one and verify your code.)
The application has a web page, so if you want to expand this code you can set up an HTML list and place the response in a <div>, and add a button on the page to invoke callAdapter(). You can also pass in any argument you want from the web page. There are lots of ways to go wrong, to get lost and to learn! As Caesar said, “Erro, errabam, errabit.” In fact, he had a type A personality, and he chose a different verb. But the result is the same …
Your turn
There is still a lot to do to get a respectable news display on your device, but the next steps go beyond Worklight, and so I shall leave it as a fun exercise for you. If you want more, you’ll have to let me know! Add a comment below and tell me what parts are still puzzling you.
|