Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. News

Introduction to webOS SDK Development: Part 3

This post is part of a series called Introduction to webOS SDK Development.
Introduction to webOS SDK Development: Part 2
Introduction to webOS SDK Development: Part 4

Welcome to the third part of our series on webOS SDK development. In part 2, we supplied static data to a list. This tutorial will show you how to load dynamic data into a list. We're going to use AJAX and YQL to achieve this.

Spinner and Scrim

Let's start by changing the list scene's HTML. Edit app/views/list/list-scene.html to contain the following:

The header and list are similar to the ones in the main scene. What we're adding is a scrim and a spinner. What is a scrim and a spinner you ask? A spinner shows a spinning (surprise!) image as an indication that an operation is in progress. It's a good choice to display a spinner for every operation that is going to take a while (and remember, since we are on a mobile device, operations that get remote data over a wireless connection might take a while). Additionally, we use a scrim (a translucent layer used to obscure the background UI) to hide the background UI while the spinner is displayed because it would make no sense to interact with the application while a operation is pending.

We also add a wrapping div around our list to push it down below the header. Define the necessary class in stylesheet/tutsplus.css for that:

Next, head over to app/assistants/list-assistant.js to add the application logic. First we define the list model. Unlike last time, where the model data was static, our listmodel won't have any data in it -it will get loaded into the list later.

List Divider

We define two new properties in our list attributes this time: dividerTemplate and dividerFunction. Let me explain dividers first. They are essentially elements put between list entries to group them. In our app we want to group the displayed articles by date. Go ahead and create the dividerFunction:

A listitem is passed to our function and we create a javascript date object out of its pubDate property (it refers to the publication date we get from the RSS feed). We then reformat that date with a Mojo function to a long date string (e.g September 6, 2010) and return that. The list logic will then use that date to group list items together that have the same date. The dividerTemplate defines how the actual divider looks. Edit app/views/list/dividerTemplate.html:

Each time the list renders a divider, it inserts the above HTML-code in replaces #{dividerLabel} with the date string.

Lets create the list template next, edit app/views/list/itemTemplate.html:

Again, we specifiy how each row of the list is laid out and what data of the model is displayed. Also add the new classes to the stylesheets/tutsplus.css:

At the end of the setup function, add the last missing piece, the setup of the spinner:

Note that we set it to spin already, but because the DIV containing the spinner is hidden we won't see the spinner image.

Allright, let's move on and edit the activate function:


We display the title-image and a call to getData. This will load the data we want to display in our list. Go ahead and add the function getData:

Before get the data, we show the DIV containing the spinner. We'll show the spinner while the load operation is in progress. Our goal is to display the lastest posts from the selected tutsplus site into our list. Each tutsplus site exports its latest articles in a RSS feed. How do we read the RSS Feed to use in our application? We're going to use YQL, the Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services (https://developer.yahoo.com/yql/). I won't go into details about YQL here, you can read more about it on nettuts.

Here's how we get the data from mobiletuts with YQL:

Use the YQL console at http://developer.yahoo.com/yql/console to try it out. Select JSON as the output format. Here is the (shortened) result:

Looks like we can use most of that data to display in our list. How do we get it into our list, you ask? AJAX is the answer. We are going to use an AJAX request to call the YQL webservice. Since mobiletuts uses a different feed than the other sites, we need to change the feed url manually.

We are using Prototype's Ajax.Request function to call the Yahoo webservice. Since AJAX calls are asynchronous, we don't know when we get back the data from the webservice. We specify the function to call when the data is received in the onSuccess callback: this.parseResult.bind(this)

There's something new in how the callback is called, notice the added statement .bind(this). Let me explain what "this" and scope in javascript means: In JavaScript, functions are executed in a specific context, referred as "scope". Inside the function the this keyword becomes a reference to that scope. For example, the variable this.title that we're using in the function getData is local to that function and won't be available in another function. Enter .bind(this). "Binding" basically determines the meaning, when a function runs, of the "this" keyword. In our example, when we call this.parseResult.bind(this), it's variables that were referenced through this are available in the parseResult function.

The data returned from the webservice call ends up in the transport object that passed to the parseResult function. We're interested in the transport.reponse Text Property, which contains the output as a JSON String. We convert that to an object by calling evalJSON. We can then loop through the properties of the JSON data and collect the data that we want to fill into our list.

Parsing JSON

Since the categories per article are dynamic, we are just taking the first 3 categories out of the JSON data and construct a new category string out of it (named cat). We also need to shorten the description, because the feed sometimes contains HTML-Strings that we don't want to display. Allright, we have parsed our response JSON and constructed a new array out of it. This array is the base for our list model.

First, we pass the array newData to the items of our list model and then notifiy the list that there's a new model ready to work with. The list will then render the list with the new data. At last, we hide our spinner to show the user that the loading process has ended.

Package the app, install and run it. For each tutsplus site you select, you should now see the list being populated with the latest articles.

Wrap up

Congratulations! We have read the contents of a RSS through YQL and fed that data into our list. In part 4 we're going to add the last missing piece to our application!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.