Ruby on Rails

Depends how you interpret things

Titanium : Passing parameters between windows [How to make things global]


While working on any titanium application, one of the common possibilities is to use some variables/constants across the windows. In Titanium, windows use to have their on context. That means the variables/values available on one window are not available on the others linked windows by default. So, if we are required to pass parameters between windows,  there are many different ways to achieve this. So, choosing a perfect approach for passing variables/parameters within windows depends on the requirement we are having. In our last project we had used few different approaches as per our requirements. Few of them are :

Ti.App.Properties:

There are few cases where we need to store values to be used everywhere in the application, and we want to retain those values even when application is not-running/ in-background. The App Properties module is used for storing application related property/value pairs which persist beyond application sessions.

We can set the property/value pair as:

Titanium.App.Properties.setString("my_prop","cool");

Now, as property/value pair is set. Now I can simply retrieve that value where I want in the application as:

var value = Titanium.App.Properties.getString("my_prop");

While setting properties in Ti.App.Properties, we have to make sure few things:
>> we are actually making things globally available, means whatever values we are storing, should not conflict with already existing global titanium constants, otherwise functionality will suffer. For more information, check Titanium documentation at Titanium.App.Properties

Ti.App:

There are cases, when we need few values to be available all over the application, but when we close the application, those values should not be available any-more. For example,  while working on an application I need to access the current locale for the device for different purposes. But when I closes the application, I don’t need that value to persist as use can change the locale in-between as well. So better to make request to get these kind of values when app initializes and persist those values throughout the application session.

We can declare methods and variables to be globally available with Ti.App as:

Ti.App.sample_variable = “xxxxxx”;

Ti.App.sample_function = function(_args){ //your code for the function };

Then we can use this variable/method anywhere in our application. It will be available in the application session.

App Level Global Declaration:

For past weeks I was wondering about what are the best ways of writing titanium javascript code to make it more generic, reusable and more modular. Tweetanium, the sample application build by titanium/appcelerator team to help people, posses a new approach for design/creation of titanium based applications. Approach is to maintain an Application Level NAMESPACE and write each and every single peace of code wither to that namespace or sub-namespaces to that. e.g.

In app.js of your application,

//app.js

//suppose name of my application is sample, so defining the global namespace or placeholder for my application

var sample = {};

Now I can define my UI, models, controllers methods and variables in the corresponding namespace or placeholder to make them more structured. As,

sample.UI = {};

Now if I want to declare any method or variables, I can place them in corresponding files and assign all the methods/ variables to their parent placeholder.

//UI.js

(function(){

sample.UI.xxxx = function(_args){

//write you code you want to use.

};

})();

For knowing more about how to write MVC structure applications with titanium, stay tuned, as I will be back soon with one more blog post for the same

Passing parameters within successive windows:

If we just need to pass parameters/values between 2 successive windows, then it can be done via sending values as a part of window object it self. Like. for example I’m working on a map related stuff and when I click on a button which leads me to the next window which shows some data corresponds to my geolocation position. Then while creating the window element we can pass those values as part of window object itself. As:

## window1.js

………

var button = Ti.UI.createButton({

label: “button1″,

height: “20dp”,

width: “100dp”

});

button.addEventListener(“touchstart”, function(){

var window2 = Ti.UI.createWindow({url:”window2.js”});

window2.sample_value = “xxxxxx”;

Ti.UI.currentTab.open(window2, {animated:true});

});

##windows2.js

var window = Ti.UI.currentWindow;

Ti.API.info(window.sample_value);

//this will return “xxxxxx” as a values;

These are the few ways of passing variables globally which I used till now. For any query Contact me anytime, or reply back.

About these ads

One response to “Titanium : Passing parameters between windows [How to make things global]

  1. mercadee 2012/11/09 at 2:26 pm

    When using Titanium we run into complications. On iOS you could use a tab group and the navbar is free. It automatically puts in the back buttons and the next button (“rightNavButton”) needs to be delivered but all in all not too bad. On Android there are tab groups but no navbar which means you’re using menus. Menus aren’t available on Android but give more UI flexibility than on iOS through having more options. The trap however is that menus and tab groups don’t work together on Android. I’m not sure if this is a Titanium issue but all I know is that it doesn’t work properly when using Titanium. The extra trap is that tab groups and the navbar works fine for iOS applications launched from Titanium’s main context (“app.js”) however on Android you can’t use a menu here. So if you want a menu on the first screen in the app you need to open another window first to create an Android activity to then associate the menu with the window.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: