The base class for Alloy controllers.
Each controller is associated with a UI hierarchy, defined in an XML file in the
views
folder. Each element in the view hierarchy is either a Titanium View
or another Alloy controller or widget. Each Alloy controller or widget can additionally contain
Titanium Views and/or more controllers and widgets.
Adds a TSS class to the specified view object.
You can apply additional styles with the opts
parameter. To use this method
effectively you may need to enable autostyling
on the target XML view. See Autostyle
in the Alloy developer guide.
The following adds the TSS classes ".redbg" and ".bigger" to a Titanium.UI.Label
object proxy label1
, and also sets the label's text
property to "Cancel".
// index.js
$.addClass($.label1, 'redbg bigger', {text: "Cancel"});
The 'redbg' and 'bigger' classes are shown below:
// index.tss
".redbg" : {
color: 'red'
}
".bigger": {
font : {
fontSize: '36'
}
}
View object to which to add class(es).
Array or space-separated list of classes to apply.
Dictionary of properties to apply after classes have been added.
Adds a tracked event listeners to a view proxy object. By default, any event listener declared in XML is tracked by Alloy.
Add an event to the tracking target.
$.addListener($.aView, 'click', onClick);
Proxy view object to listen to.
Name of the event.
Callback function to invoke when the event is fired.
ID attribute of the view object. If one does not exist, Alloy will create a unique ID.
Creates a dictionary of properties based on the specified styles.
You can use this dictionary with the view object's applyProperties method or a create object method, such as Titanium.UI.createView.
The following creates a new style object that is passed as a parameter to the Ti.UI.createLabel() method.
var styleArgs = {
apiName: 'Ti.UI.Label',
classes: ['blue','shadow','large'],
id: 'tester',
borderWidth: 2,
borderRadius: 16,
borderColor: '#000'
};
var styleObject = $.createStyle(styleArgs);
testLabel = Ti.UI.createLabel(styleObject);
The next example uses the applyProperties() method to apply a style object to an existing Button control (button not shown).
var style = $.createStyle({
classes: args.button,
apiName: 'Button',
color: 'blue'
});
$.button.applyProperties(style);
Dictionary of styles to apply.
Frees binding resources associated with this controller and its UI components. It is critical that this is called when employing model/collection binding in order to avoid potential memory leaks. $.destroy() should be called whenever a controller's UI is to be "closed" or removed from the app. See the Destroying Data Bindings test application for an example of this approach.
In the following example the view-controller for a Window object named dialog
calls its destroy()
method in response to the Window object being closed.
$.dialog.addEventListener('close', function() {
$.destroy();
});
Gets all the tracked event listeners of the view-controller or
only the ones specified by the parameters. Passing no parameters,
retrieves all tracked event listeners. Set a parameter to null
if you do not want to restrict the match to that parameter.
Get all events bound to the view-controller.
var listener = $.getListener();
Proxy view object.
Name of the event.
List of tracked event listeners.
Returns a list of the root view elements associated with this controller.
The following example displays the id
of each top-level view associated with the
controller:
// index.js
var views = $.getTopLevelViews();
for (each in views) {
var view = views[each];
console.log(view.id);
}
Returns the specified view associated with this controller.
If no id
is specified, returns the first top-level view.
The following example gets a reference to a <Window/>
object
with the id
of "loginWin" and then calls its open() method.
var loginWindow = $.getView('loginWin');
loginWindow.open();
ID of the view to return.
Returns a list of all IDed view elements associated with this controller.
Given the following XML view:
<Alloy>
<TabGroup id="tabs">
<Tab title="Tab 1" icon="KS_nav_ui.png" id="tab1">
<Window title="Tab 1" id="win1">
<Label id="label1">I am Window 1</Label>
</Window>
</Tab>
<Tab title="Tab 2" icon="KS_nav_views.png" id="tab2">
<Window title="Tab 2" id="wind2">
<Label id="label2">I am Window 2</Label>
</Window>
</Tab>
</TabGroup>
<View id="otherview"></View>
</Alloy>
The following view-controller outputs the id of each view in the hierarchy.
var views = $.getViews();
for (each in views) {
var view = views[each];
console.log(view.id);
}
[INFO] : win1
[INFO] : label1
[INFO] : tab1
[INFO] : wind2
[INFO] : label2
[INFO] : tab2
[INFO] : tabs
[INFO] : otherview
Removes a TSS class from the specified view object.
You can apply additional styles after the removal with the opts
parameter.
To use this method effectively you may need to enable autostyling
on the target XML view. See Autostyle
in the Alloy developer guide.
The following removes the "redbg" and "bigger" TSS classes from a Titanium.UI.Label
object proxy label1
, and also sets the label's text
property to "...".
$.removeClass($.label1, 'redbg bigger', {text: "..."});
View object from which to remove class(es).
Array or space-separated list of classes to remove.
Dictionary of properties to apply after the class removal.
Removes all tracked event listeners or only the ones
specified by the parameters. Passing no parameters,
removes all tracked event listeners. Set a parameter to null
if you do not want to restrict the match to that parameter.
When the window is closed, remove all tracked event listeners.
<Alloy>
<Window onOpen="doOpen" onClose="doClose">
<Label id="label" onClick="doClick">Hello, world</Label>
</Window>
</Alloy>
function doClose() {
$.removeListener();
}
Proxy view object to remove event listeners from.
Name of the event to remove.
Callback to remove.
Controller instance.
Sets the array of TSS classes for the target View object, adding the classes specified and removing any applied classes that are not specified.
You can apply classes or styles after the reset using the classes
or opts
parameters.
To use this method effectively you may need to enable autostyling
on the target XML view. See Autostyle
in the Alloy developer guide.
The following removes all previously applied styles on label1
and then applies
the TSS class 'no-style'.
$.resetClass($.label1, 'no-style');
View object to reset.
Array or space-separated list of classes to apply after the reset.
Dictionary of properties to apply after the reset.
Applies a set of properties to view elements associated with this controller. This method is useful for setting properties on repeated elements such as TableViewRow objects, rather than needing to have a controller for those child controllers.
The following example uses this method to update a Label inside a TableViewRow object before adding it to a TableView.
View-controller file: controllers/index.js
for (var i=0; i < 10; i++) {
var row = Alloy.createController("tablerow");
row.updateViews({
"#theLabel": {
text: "I am row #" + i
}
});
$.tableView.appendRow(row.getView());
};
XML view: views/tablerow.xml
<Alloy>
<TableViewRow>
<Label id="theLabel"></Label>
</TableViewRow>
</Alloy>
XML view: views/index.xml
<TableView id="tableView">
</TableView>
An object whose keys are the IDs (in form '#id') of views to which the styles will be applied.
this