Book Image

Appcelerator Titanium Smartphone App Development Cookbook

By : Boydlee Pollentine
Book Image

Appcelerator Titanium Smartphone App Development Cookbook

By: Boydlee Pollentine

Overview of this book

<p>Appcelerator Titanium Mobile allows developers to realize their potential to develop full native iPhone and Android applications by using free Titanium Studio tools without the need to know Objective-C or Java. This practical hands-on cookbook shows you exactly how to leverage the Titanium API to its full advantage and become confident in developing mobile applications in no time at all.<br /><br />Appcelerator Titanium Smartphone App Development Cookbook offers a set of practical and clear recipes with a step-by-step approach for building native applications for both the iPhone and Android platforms using your existing knowledge of JavaScript.<br /><br />This cookbook takes a pragmatic approach to using your JavaScript knowledge to create applications for the iPhone and Android platforms, from putting together basic UIs to handling events and implementation of third party services such Twitter, Facebook and Push notifications. This book shows you how to utilize both remote and local datasources using XML, JSON and the SQLite database system. The topics covered will guide you to use popular Titanium Studio tools effectively and help you leverage all the advanced mobile features such as Geolocation, Accelerometer, animation and more. Finally, you’ll learn how to register developer accounts and how to publish your very own apps to the Android and Apple marketplaces.</p>
Table of Contents (18 chapters)
Appcelerator Titanium Smartphone App Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Passing custom variables between windows


You will often find a need to pass variables and objects between different screen objects, such as Windows, in your apps. One example is between a master and child view. For example, if you have a tabular list of data that perhaps only shows a small amount of information per row and you wish to view the full description, you might pass that description data as a variable to the child window.

In this recipe, we are going to apply that same principle to a variable on the settings window (in the second tab of our LoanCalc app), by setting the variable in one window and then passing it back for use in our main window.

Getting ready

The complete source code for this recipe can be found in the /Chapter 1/Recipe 7 folder.

How to do it...

Under the declaration for your second window (win2) in your app.js file, add the following additional property called autoShowChart and set it to false. This is a custom property, that is, a property not already defined by the Titanium API. It is often handy to add additional properties to your objects if you require certain parameters that the API does not provide by default:

//
//////set the initial value of win2's custom property
win2.autoShowChart = false;

Now in the window2.js file that holds all of the sub components for your second window, add the following code extending the existing Switch control so it can update the referenced window's autoShowChart variable:

//create the switch object
var switchChartOption = Titanium.UI.createSwitch({
  right: 20,
  top: 20,
  value: false
});

//add the event listener for the switch when it changes
switchChartOption.addEventListener('change', function(e){
  win2.autoShowChart = switchChartOption.value;
});

//add the switch to the view
view.add(switchChartOption);

How it works…

This code is actually pretty straightforward. When an object is created in Titanium, all of the standard properties are accessible in a dictionary object of key-value pairs. All we are doing here is extending that dictionary object to add a property of our own.

We can do this in one of the two ways. First, as shown in our recipe's source code this can be done after the instantiation of the Window (win2) object. Second, it can also be done immediately within the instantiation code. In the source code of the second window, we are simply referencing this same object, so all of its properties are already available for us to read from and write to.

There's more...

There are other ways to pass and access objects and variables between Windows, including the use of App Properties. These will be covered in a later chapter.