Alloy.Controller
> Alloy.Controller

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.

Defined By

Methods

Alloy.Controller
( proxy, classes, [opts] )
Adds a TSS class to the specified view object. ...

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.

Example

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'
    }
}

Since 1.2.0

Parameters

  • proxy : Object

    View object to which to add class(es).

  • classes : Array<String>/String

    Array or space-separated list of classes to apply.

  • opts : Dictionary (optional)

    Dictionary of properties to apply after classes have been added.

Returns

  • void
Alloy.Controller
( proxy, type, callback ) : String
Adds a tracked event listeners to a view proxy object. ...

Adds a tracked event listeners to a view proxy object. By default, any event listener declared in XML is tracked by Alloy.

Example

Add an event to the tracking target.

$.addListener($.aView, 'click', onClick);

Since 1.7.0

Parameters

  • proxy : Object

    Proxy view object to listen to.

  • type : String

    Name of the event.

  • callback : Function

    Callback function to invoke when the event is fired.

Returns

  • String

    ID attribute of the view object. If one does not exist, Alloy will create a unique ID.

Alloy.Controller
( opts ) : Dictionary
Creates a dictionary of properties based on the specified styles. ...

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.

Examples

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);

Since 1.2.0

Parameters

Returns

Alloy.Controller
( )
Frees binding resources associated with this controller and its UI components. ...

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.

Example

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();
});

Returns

  • void
Alloy.Controller
( [proxy], [type] ) : Array<TrackedEventListener>
Gets all the tracked event listeners of the view-controller or only the ones specified by the parameters. ...

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.

Example

Get all events bound to the view-controller.

var listener = $.getListener();

Since 1.7.0

Parameters

  • proxy : Object (optional)

    Proxy view object.

  • type : String (optional)

    Name of the event.

Returns

Alloy.Controller
( ) : Array.<(Titanium.UI.View|Alloy.Controller)>
Returns a list of the root view elements associated with this controller. ...

Returns a list of the root view elements associated with this controller.

Example

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

Alloy.Controller
( [id] ) : Titanium.UI.View/Alloy.Controller
Returns the specified view associated with this controller. ...

Returns the specified view associated with this controller.

If no id is specified, returns the first top-level view.

Example

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();

Parameters

  • id : String (optional)

    ID of the view to return.

Returns

Alloy.Controller
( ) : Array.<(Titanium.UI.View|Alloy.Controller)>
Returns a list of all IDed view elements associated with this controller. ...

Returns a list of all IDed view elements associated with this controller.

Example

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

Returns

Alloy.Controller
( proxy, classes, [opts] )
Removes a TSS class from the specified view object. ...

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.

Example

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: "..."});

Since 1.2.0

Parameters

  • proxy : Object

    View object from which to remove class(es).

  • classes : Array<String>/String

    Array or space-separated list of classes to remove.

  • opts : Dictionary (optional)

    Dictionary of properties to apply after the class removal.

Returns

  • void
Alloy.Controller
( [proxy], [type], [callback] ) : Alloy.Controllerchainable
Removes all tracked event listeners or only the ones specified by the parameters. ...

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.

Example

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();
}

Since 1.7.0

Parameters

  • proxy : Object (optional)

    Proxy view object to remove event listeners from.

  • type : String (optional)

    Name of the event to remove.

  • callback : Function (optional)

    Callback to remove.

Returns

Alloy.Controller
( proxy, [classes], [opts] )
Sets the array of TSS classes for the target View object, adding the classes specified and removing any applied class...

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.

Example

The following removes all previously applied styles on label1 and then applies the TSS class 'no-style'.

$.resetClass($.label1, 'no-style');

Since 1.2.0

Parameters

  • proxy : Object

    View object to reset.

  • classes : Array<String>/String (optional)

    Array or space-separated list of classes to apply after the reset.

  • opts : Dictionary (optional)

    Dictionary of properties to apply after the reset.

Returns

  • void
Alloy.Controller
( args ) : Alloy.Controllerchainable
Applies a set of properties to view elements associated with this controller. ...

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.

Example

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>

Since 1.4.0

Parameters

  • args : Object

    An object whose keys are the IDs (in form '#id') of views to which the styles will be applied.

Returns