Kevin Nelson Marshall
Other entries:
« A simple idea from a simple man

I spent a lot of today cleaning out our garage (yuck) and doing other 'offline' things. There seems to be a Sat. trend developing here!

Anyway, since I don't have much to report in terms of 'new' development, and since MySpace just made their app network live, I figured I would take a minute to break down a simple MySpace app.

If you want to play along with this example, you'll have to first get approved to be a MySpace App Developer. It's pretty easy but it can take a few days because (as I write this) they are manually approving all applications.

So moving along and assuming you have access to develop a MySpace application, let's get into some of the details of building a HTML/Javascript MySpace app (using OpenSocial).

First, there are currently three places you can write code for your MySpace app. The canvas, the profile, and the homepage.

The canvas is intended to be the main page/area of your application. This is where all the real action (ie. the real program that is your app) is intended to run. You pretty much get all the space on the page to play with.

The profile is for a section of space that will show up on your app user's profile page. This is generally intended for basic notifications and simple controls of your application. This is the MySpace page that a user who installs your app will see most often as it's the area that they control their MySpace page(s) from.

Finally the homepage is for a section of space that will show up on your app user's homepage - that is the page that everyone else sees when they visit someone's MySpace page. This is generally intended for basic notifications or the 'front-facing' bits of your application (ie. the things you want the general public to see as a result of your user having the app installed on their page).

The example I'm going to dump on you could really be used in any of these three areas, but for my testing I wrote it for the canvas section. Since it doesn't really do anything all that exciting that other people would care about, you should probably do the same.

OK with all of that out of the way, let's get into the code deatils already!

When it comes to coding your app., the most important thing to remember is that all we can do is HTML and Javascript. Luckily, that gives us enough power to do just about anything we really want. In our example, all we are going to do list out the app owner's friends. But trust me, there is a lot more you can do with this stuff (maybe down the road I'll get into some more of the interesting things for you).

With all that in mind, let's start with the basic HTML of our example. We define a header, and a simple div tag. This div tag is what we will manipulate via our Javascript later.

<h1>Your Friends &raquo;</h1>
<div id="friends"></div>


Next up is our Javascript (where all the real action occurs). The basic idea behind the MySpace (and OpenSocial) Javascript features is to define actions and callbacks. We start with a init function (a common initialize type of function), within that we'll define a new opensocial data request, set up the request to get a list of our app installer's friends, and finally make the actual request.

<script>
function init() {
  var dataRequest = opensocial.newDataRequest();
  var friendRequest = dataRequest.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS);
  dataRequest.add(friendRequest);
  dataRequest.send(responseCallback);
}
</script>


I think it's all pretty self-explanitory, but I do feel like I need to point out that last little bit - the dataRequest.send method. That method is actually making the call and then passing back the results (asynchronously or rather whenever it can regardless of what else is going on on the page) to our second function (ie. the call back function).

So now that we know we are sending results to another function, we have to actually write that callback bit. That is, write the code that deals with the list of opensocial friend objects we'll get back. Again it's pretty simple. Here we put the JSON (all the opensocial and myspace calls return JSON formatted data) response into a simple Javascript variable. Then we loop through the results, getting the name value for each of our friends and appending it to a variable (our running list of results). Once we've done we just update the innerHTML of our div tag with the variable we just populated.

<script>
function responseCallback(dataResponse) {
  var friendsData = dataResponse.get(opensocial.DataRequest.Group.OWNER_FRIENDS).getData();
  var friendlist = "";
  var friendcount = 0;
  friendsData.each(
    function(friendData) {
      var friendName = friendData.getField(opensocial.Person.Field.NAME);
      friendcount++;
      friendlist += friendcount + ". " + friendName + "<br>";
    }
   );
  friends.innerHTML = friendlist;
}
</script>


We're almost done. The only thing left to do is actually kick off the execution of all this (did you notice we haven't done that yet?). We do that with a simple call to our init() function, like so:

<script>
init();
</script>


There you have it, a simple little MySpace app that lists out your friends (with a basic counter). Nothing too fancy, but hopefully it shows you how easy this stuff can be (once you learn the details of the OpenSocial and MySpace features like dataRequest).

Below is the full code that I broke down above (for easy copy/paste). Go ahead and dump it into your app canvas source, save it, and give it a run...it *should* list out your friends just like we wanted.

<h1>Your Friends »</h1>
<div id="friends"></div>

<script>
function init() {
  var dataRequest = opensocial.newDataRequest();
  var friendRequest = dataRequest.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS);
  dataRequest.add(friendRequest);
  dataRequest.send(responseCallback);
}

function responseCallback(dataResponse) {
  var friendsData = dataResponse.get(opensocial.DataRequest.Group.OWNER_FRIENDS).getData();
  var friendlist = "";
  var friendcount = 0;
  friendsData.each(
    function(friendData) {
      var friendName = friendData.getField(opensocial.Person.Field.NAME);
      friendcount++;
      friendlist += friendcount + ". " + friendName + "<br>";
    }
   );
  friends.innerHTML = friendlist;
}

init();
</script>

posted by Kevin Marshall on 2008-03-15 00:00:00+00


Subscribe to my RSS feed »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk Kevin on »

bit.ly
Delicious
Digg
Disqus
Facebook
Flickr
FriendFeed
Github
Last.fm
LinkedIn
StumbleUpon
Twitter (@falicon)

Archives by Category »

(28) Code »
(8) ColdFusion »
(15) Database »
(10) Factor »
(3) Falcons »
(321) General »
(13) JavaScript »
(18) Perl »
(17) PHP »
(20) Ruby »

Archives by Month »

(1) September 2010 »
(2) August 2010 »
(3) July 2010 »
(13) June 2010 »
(8) May 2010 »
(2) April 2010 »
(2) March 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0



This blog is powered by KickAssCode.