Instantiated windows and controls in Titanium Appcelerator act like JavaScript objects. JavaScript allows you to create custom properties in objects even when that property is not defined. You can take advantage of this in Titanium to pass some information to a window that you are about to open.
For example, on a clean project, you can have this on your app.js
:
Above, we are setting a custom property named username
to the mywindow
variable which is an instance of Titanium.UI.Window
. We can now access the value of
username
in mywindow.js
:
The output would be a window with a textbox whose default value is “John Doe”. That was the value we set in app.js
.
Not Just Scalar Values
You’re not limited to scalar values like what I just showed. You can also set full JavaScript objects:
And access them similarly:
There is one caveat when setting objects. When setting a property to an object, you won’t be able to modify that object’s properties directly:
You’ll have to set the property again to your modified object. You can do it like this:
Also applicable to controls
Remember that you can also set custom properties on controls:
It comes in handy sometimes.
Final words
Watch out when setting custom properties. Make sure that the property name doesn’t exist. For this reason I prefer to set objects to a single custom property
(e.g. mywindow.params
).