×

HELLO, MY
NAME IS MARTIN
AND THIS IS
MY BLOG

Working with required views and windows in Titanium Alloy

Posted on

Requiring views in alloy markup is a perfect way to keep your code separated and maintainable. With alloy MVC, you will reference an element of a view file from the corresponding controller most of the time. But there are certain occasions, where you would like to have access to an element inside the required view. Look at the following example:

<Alloy>
    <NavigationWindow>
      <Require type="view" src="myRequiredView" id="myRequiredView"/>
    </NavigationWindow>
</Alloy>
<Alloy>
    <Window id="myRequiredWindow">
       <View id="myAnimationView" />
    </Window>
</Alloy>

So how would you reference “myAnimationView” from index.js, which is not the corresponding controller of “myRequiredView”?

Working with jQuery and CSS selectors in the web-world, one could be tempted to try

var myAnimationView = $.myRequiredView.myAnimationView
or even
var myAnimationView = $.myRequiredView.myAnimationWindow.myAnimationView

Although the Titanium documentation is very clear on this, a trial and error approach often looks like this. With limited success.

So, here is how it is done. It is simple and yet, you have to know it

var myAnimationView = $.myRequiredView.getView('myAnimationView');