Getting Started

You’ll find everything you need to get started in developing Palm webOS applications at webOSdev, the Palm developer site (http://developer.palm.com). You’ll need to sign up as a Palm developer and download the SDK. There are options for Mac OS X, Windows XP/Vista, and Linux, so download the SDK package that matches your development platform and run the installer.

The installation will put a copy of the SDK, including the Palm Mojo framework and Palm development tools, into one of the project directories listed in Table 2-1.

Table 2-1. SDK installation directories

Platform

Location

Mac OS X

/opt/PalmSDK/Current/

Windows XP/Vista

C:Program FilesPalmSDK

Linux

/opt/PalmSDK/Current

The installer will give you the option of installing different tool bundles. The tools package includes a collection of command-line tools which can be run on all platforms. In addition, the tools have been integrated into some popular IDEs and web development editors. Check the developer portal for an up-to-date list of bundles and supported editors.

The application samples in this book were all developed on a Mac with TextMate and the command-line tools. The command-line option for the tools will be shown in the examples and is the same on every platform. If you are using Eclipse/Aptana or another tool bundle, there should generally be direct menu options for the commands used in the book. In some cases, several commands may be combined into one menu option.

Creating Your Application

Palm webOS applications have a standard directory structure with naming conventions and some required files. To help you get started quickly, the SDK includes palm-generate, a command-line tool that takes an application or scene name as arguments and creates the required directories and files. You can run this from the command line (the $ is the command-line prompt and should not be entered as part of the command):

$ palm-generate AppName

Note

The command-line tools all work off of the current directory. You should change the directory to a projects directory (or wherever your workspace is located) before running the tools.

The tool creates a functional application within a conventional webOS directory structure. Every webOS application should have a directory structure similar to the following, and some parts of the structure are required:

AppName
   -> app
       -> assistants
           -> first-assistant.js
           -> second-assistant.js
           -> ...
       -> views
           -> first
               -> first-scene.html
           -> second
               -> second-scene.html
           -> ...
   -> appinfo.json
   -> icon.png
   -> images
       -> image_1.png
       -> image_2.jpg
       -> ...
   -> index.html
   -> sources.json
   -> stylesheets
       -> AppName.css
       -> ...

You are free to choose any project directory name, but AppName should correspond to the value of the id property in appinfo.json, discussed later in this section. An application’s logic and presentation files reside in the app directory, which provides a directory structure loosely based on the MVC pattern.

There are scene assistants in the assistants directory of your application. As discussed earlier, a scene is a particular interactive screen in an application. Scene assistants are delegates implemented in your application, and are used by the framework’s controllers to customize an application’s behavior.

All layout files are located in the views directory of your application. A scene assistant has one main HTML view file, which provides the structure and content of its presentation page. It also includes optional HTML template view files that may be used to display dynamic data, such as JavaScript object properties for UI controls. These files are fragments of the UI that are combined together by the framework to produce the final presentation of the scene.

An application’s images are located in the images directory and the CSS files are placed in the stylesheets directory. As with web applications, webOS applications use HTML to structure the layout of a page and CSS to style the presentation. CSS files are used to style your custom HTML, and you can also use CSS to override Mojo’s default styles.

The appinfo.json object gives the system the information it needs to load and launch your application. Within appinfo.json there are both required and optional properties, which are described in Table 2-2.

The palm-generate tool creates an appinfo.json file with a common set of properties set to default values. The most important property is the id, which must be unique for each application; it’s used in service calls to identify the caller and serves as a unique application identifier.

Table 2-2. appinfo.json properties and values

Property

Values

Required

Description

title

any

Yes

Name of application as it appears in Launcher and in application window

type

web

Yes

Conventional application

main

any

Yes

Application entry point

id

any

Yes

Must be unique for each application

version

x.y.z

Yes

Application version number

vendor

any

Yes

A string representing the maker of the application; it is used in launcher and deviceinfo dialogs

vendorurl

any

No

A string representing a URL that turns the vendor portion in deviceinfo dialogs to hyperlinks

noWindow

true/false

No

Background application; defaults to false

icon

file path

No

Application’s launcher icon; defaults to icon.png

miniicon

file path

No

Notification icon; defaults to miniicon.png

Warning

A warning about syntax:

  • Don’t include any comments in appinfo.json files (/* or //).

  • Must use double quotes around properties—no single quotes!

  • Strict JSON parser; this file must follow all the rules for correct JSON.

The Application Manager is responsible for putting the application in the Launcher using the icon.png as the icon for your application. Application icons should be 64 × 64 pixels, encoded as a PNG with 24 bit/pixel RGB and 8 bits alpha. The icon’s image should be about 56 × 56 pixels within the PNG bounds.

Note

Refer to the webOS style guidelines found in the SDK documentation for more information about icon design.

Following web standards, index.html is the first page loaded when a webOS application is launched. There are no restrictions on what you put into the index.html, but you do need to load the Mojo framework here. Include the following in the header section:

<script src="/usr/palm/frameworks/mojo/mojo.js"
    type="text/javascript" x-mojo-version="1"></script>

This code loads the framework indicated by the x-mojo-version attribute; in this case version 1. You should always specify the latest version of the framework that you know is compatible with your application. If needed, Palm will include old versions of the framework in webOS releases, so you don’t need to worry about your application breaking when Palm updates the framework.

You can load your JavaScript using the sources tag in index.html, but this will load all the JavaScript at application launch. To improve launch performance, it is recommended that you use sources.json to provide lazy loading of the JavaScript. The palm-generate tool will create a template, and you can add the application specific files as they are created.

The generated file includes only your stage assistant, but a typical sources.json includes some scene assistants and perhaps an app assistant:

[
  {
    "source": "app/assistants/app-assistant.js"
  },
  {
    "source": "app/assistants/stage-assistant.js"
  },
  {
    "source": "app/assistants/first-assistant.js",
    "scenes": "first"
  },
  {
    "source": "app/models/data.js",
    "scenes": ["first", "second"]
  },
  {
    "source": "app/assistants/second-assistant.js",
    "scenes": "second"
  }
]

The app-assistant file path comes first, followed by the stage-assistant and the scenes file paths after that. The scene file paths can be in any order, but must include both the source and scenes properties. Note the example where both the first and second scenes with a dependency on the same file. HTML files are not included.

Applications can add other directories to the structure above. For example, you might put common JavaScript libraries under a javascripts or library directory, or put test libraries under tests. The required elements are:

  • The app directory and everything within it

  • appinfo.json

The rest of the structure and naming is recommended but not required.

Testing and Debugging

Most web applications can simply be loaded into a browser to run and debug them, and webOS apps can also be tested and debugged that way. However, you’ll run into difficulty if your application is using Mojo widgets or webOS services. And it’s difficult to fully test your application without seeing it working within the webOS System UI and other applications.

For this reason, you’ll want to use the webOS Emulator with integrated JavaScript debugger and DOM inspector. Unlike the other development tools, the emulator is a full native application on every platform and will be found in the Applications directory on MacOS X and Linux or in the Programs directory on Windows XP/Vista. First, you can launch the emulator directly; it will bring up a window that looks like a Palm Prē. Or, you can use command-line tools; use palm-package to package your application and use palm-install to run it on the emulator.

You’ll also use the debugger for testing your applications after connecting any Palm webOS device to your system using a USB cable. From the command line you can run palm-package and palm-install to run your application on the device as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.193