titaniumtitanium-mobiletitanium-alloyappcelerator-titaniumtitanium-android

Titanium alloy : get current window from required view


I have following code (index.js) :

<Alloy>
  <Window id="home" >
    <View id="formulaire">
	  <Require type="view" id="etape_1_form" src="form/etape_1" />
	</View>
  </Window>
</Alloy>

On etape_1_form.xml i have to use a picker widget from : danielhanold.pickerwidget

here is a simple method to use this widget inside etape_1_form.js (the controller) :

Alloy.createWidget('danielhanold.pickerWidget', {
  id: 'mySingleColumn',
  outerView: $.home,
  hideNavBar: false,
  type: 'single-column',
  selectedValues: [20],
  pickerValues: [{10: 'Auburn', 20: 'Bald', 30: 'Black', 40: 'Blond', 50: 'Brown'}],
  onDone: function(e) {
    // Do something
  },
});

$.form_win.open();

Now instead of opening a picker, this throw an error because i cannot access the current window from this line :

outerView: $.home
The error : "undefined is not an object (evaluating 'outerView.add')

When i move the picker to the index.js (main controller) the picker works, but i have many required forms and want to organise my code, so i like put each form js code inside it's controller file.

So how i can access $.home window from required view? thank you for your help.


Solution

  • No, you can't directly.

    But you can easily avoid this using "globals".

    On index.js:

    Alloy.Globals.indexController = $;
    

    On etape_1_form.js

    outerView: Alloy.Globals.indexController.home