Chapter 6. Getting to Grips with Events and Properties

In this chapter, we will cover:

  • Reading and writing app properties
  • Firing and capturing events
  • Passing event data between your app and a WebView using custom events

Introduction

This chapter describes the processes of two fundamentally important, yet deceptively simple, parts of the Titanium framework. In this chapter, we'll explain how to go about creating and reading app properties so that you can store data that is accessible from any part of your application. This is similar to how session data or cookies would work if you were building a web-based app.

We'll also go into further detail on events, including a selection of those fired by the various components in Titanium, and custom events that you can define yourself.

Application properties are used for storing data in key/value pairs. These properties can persist between your app's windows, and even beyond single application sessions, much like a website cookie. You can use any combination of upper case or lower case letters and numbers in a property name, but you should mix them with care as JavaScript is case-sensitive. In this regard, Myname, myname, and MYNAME would be three very different properties!

When should you use app properties?

Application properties should be used when one or more of the following points are true:

  • The data consists of simple key/value pairs
  • The data is related to the application rather than the user
  • The data does not require other data in order to be meaningful or useful
  • There only needs to be one version of the data stored at any one time

For example, storing a string/string key pair of data_url and "http://mywebsite.com/data.xml" would be a valid way to use app properties. This URL could be re-used throughout all of your application screens/windows and is related to your application, rather than your data.

If your data is complex and needs to be joined, ordered, or queried when retrieving it, then you are better off using a local database built with SQLite. If your data is a file or large blob object, (for example, an image) then this is better stored on the filesystem, in the manner described in our previous recipe.

What object types can be stored as app properties?

There are currently five distinct types of objects that can be stored in the app properties module. These include:

  • Booleans
  • Doubles (float values)
  • Integers
  • Strings
  • Lists (arrays)

In the following recipe, we will create a number of app properties and then read them back, printing them out to the console as we do so.

Note

Complete source code for this entire chapter can be found in the /Chapter 6/EventsAndProperties.

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

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