As a new Pre owner, I was curious to learn to code for the Palm WebOS SDK (Mojo). Whenever I try to learn a new technology, I try to build a functional test project. So… what to build for WebOS? Since the Pre’s web browser doesn’t currently support the W3C Geolocation spec used by Flickr Nearby, I figured a good “Hello World” might be an application to get the GPS coordinates from the Pre, and then load the appropriate Flickr page for that location.
To begin, follow one of the existing tutorials out there for setting up your environment, learning the basic concepts, generating the project and first scene, etc. etc. I’m just going to show here what I did from when I actually started coding.
First, we’ll need some minor code to push our main “scene” to the stage.
1 2 3 4
Let’s give the scene some content:
1 2 3 4 5 6 7 8 9 10 11
Note I’m not really bothering with too much of the special Mojo chrome here, but that’s fine, right? Yay for web standards! Let’s perhaps add some CSS to prettify stuff:
1 2 3 4 5 6 7 8 9 10 11 12
Here’s what that scene will look like when it’s first loaded:
Now we get to the meat of the application, configuring the “assistant” for the scene. Which will be in
/app/assistants/main-assistant.js for those following along at home.
First, in the setup method, I’m going to initialize the mojo widget for a spinner (mojo widgets are basically chrome that have special properties and methods built in.) Next, I’ll fire off a services request to the location manager to get info from the GPS, and bind it to callback handlers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The services request is an asynchronous call, so I have to create a function to catch the response when it comes back. Here I define that method, which reads the response and updates my interface accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
And finally, here’s my method (separated out for code simplicity) that does something with the final coordinates. If I was being fancy, I would make some calls to the Flickr API and retrieve information about photos and build an interface, but it just so happens Flickr already has a nice webkit optimized webpage to display that information, so we’ll just use the browser:
1 2 3 4 5 6 7 8 9 10 11 12 13
(Note, we could have also done this in an embedded webview, but I figured it was more polite to open a “real” browser window so the user could use their bookmarks etc.)
I was pretty impressed with how easy it was to get running. Since this was a “trivially simple” example, I didn’t really get into the guts of what Mojo can do. There’s lots of neat stuff with system notifications, background applications, cloud services and other things that if you’re serious about I’d suggest checking out something like Mitch Allen’s book.
UPDATE: You can now get the source for this on github.