Passing parameters from currentWindow to the new window in Titanium
There are many ways of making a variable available in different windows in Titanium but the usage of different methods depends on the scope of the variable.
In the following code, variable ‘message’ and function ‘greetings’ are passed to the new window ‘window.js’ which is opened on click of ‘Show Message’ button.
app.js
var window = Ti.UI.createWindow();
var messageTxt = Titanium.UI.createTextField({
color:'#336699',
height:35,
top:10,
left:10,
width:250,
hintText: "Enter message"
});
var btn = Ti.UI.createButton({title: 'Show Message', height:50, width:200});
window.add(messageTxt, btn);
btn.addEventListener('click',function(e)
{
var win = Ti.UI.createWindow({
url:'window.js'
});
var message = messageTxt.value;
function greetings(){
return "Hello World!";
}
//To give the new window access to both variable 'message' and function 'greetings', we
//need to assign them to the new window reference win. These variables are only
//available to the new window win.
win.message = message;
win.greetings = greetings;
});
window.open();
window.js
var currentwindow = Ti.UI.currentWindow;
alert("message = " + currentwindow.message);
alert("greeting() = " + currentwindow.greetings());
Since these are the references, any changes made to these Javascript references will be immediately available in all sub-contexts.
Some other ways of passing variables are :
1. Titanium.App
This is mainly used to store values which need to be accessed at runtime throughout the application session.
Usage:
Ti.App.message = “Hello World!”
Value can be accessed with: Ti.App.message
2. Titanium.App.Properties
Used for storing application related property/value pairs which persist beyond application sessions.For e.g boolean value for accepted/rejected terms and conditions.
usage:
Ti.App.Properties.setString(‘message’, “Hello World!”)
Ti..App.Properties.getString(‘message’)
Please follow the documentation for details on Titanium.App and Titanium.App.Properties.
Awesome! awesome! Keep sharing!
harisiyer
April 11, 2011 at 6:40 am
Sure.. Thanks !
Sushma
April 11, 2011 at 8:35 am
Thanks! It’s working. Keep sharing such a good things….
nilkash
September 12, 2011 at 5:57 am