Unable to change parent Window Label from Modal Window

You must Login before you can answer or comment on any questions.

In iPhone when modal window dismiss so I am getting callback for 'focus' but in android focus is not called when modal window dismiss.

btn1.addEventListener('click',function(e){
 
    var w = Titanium.UI.createWindow({
        backgroundColor:'#FFFFFF',
        title:'Select Location',
        barColor:'black',
        barImage: '../images/bkg-header.png',
        url:'weather_cities.js',
        exitOnClose:true,
    });
    w.open({modal:true});
 
    w.addEventListner('close',function(e) {
 
        var mycity = Titanium.App.Properties.getString('City');
        this.cityLabel.text = mycity;  // unable to change cityLabel value
    });
});
From Modal Window, which is opened from Parent Window.
var win = Titanium.UI.currentWindow;
 
b.addEventListener('click',function()
    {
        setTimeout(function() { win.close(); }, 1000);
 
            win.addEventListener('close', function() {
                alert('Close event fired');
                Ti.API.info('Close event fired');
                Ti.API.debug(win);
 
               //var mycity = Titanium.App.Properties.getString('City');
                //super.cityLabel.text = mycity;
 
            });
    });

— asked 10 months ago by Umaid Saleem
2 Comments
  • Hi Umaid

    I am going to have to make a few assumptions as your code appears to be missing some of the code used to connect to your event listeners. I will 'assume' btn1 & b are both standard buttons.

    There are a number of issues you have created that are not helping you solve your own problem.

    In your first example you add the event listener AFTER the window is opened, which could allow the event to not be ready to respond when the actual event is fired. Move the w.open... line below the event listener.

    In the first example - you have misspelled addEventListener as addEventListner.

    You refer to this which can be ambiguous and easily lost in the scope of your code.

    Consider revising your code to use 'setters' and 'getters' cityLabel.setText(mycity) & mycity = cityLabel.getText() instead of properties cityLabel.text = mycity when setting or reading values, this is now considered the correct way and I suspect as some point in the future the properties method will only be available during object creation and not afterwards.

    Your second example fires a close request on a time-out (not sure why), however you do not add the event listener until after you set-up the time-out. Move the win.addEventListener.. section OUTSIDE the b.addEventListener... function. That way it is set up and ready to listen BEFORE you call it. Chances are your time-out can be removed as well, as I imagine you were playing with the timings of the close hoping that would help.

    Finally you are using the window property url. This is where your problems may lay. This method was how Titanium started off suggesting coders manage projects and windows, the technique is used HEAVILY in the Kitchen Sink app. However that technique has fallen out of favour for many reasons including; scope issues and the underpinnings of the system having to spawn new instances for each context.

    So you can solve your actual problem two ways.

    1. Abandon the method of using url and then with careful use of variables and Name Spaces you can directly reference the view, label etc you want. as in lblCity.setText(myCity). See Mobile Best Practices: Applications shall run in a single JavaScript context
    2. Use custom event listeners and custom fire events, these are powerful but should be used sparingly. See Event Handling: Considerations and best practices
      // sets a custom event ready to listen for your event to fire later
      // add AFTER you have created both the window and the label
      Ti.APP.addEventListener('updatecitylabel', function (newCaption) {
          lblCity.setText(newCaption);
      });
       
       
      //elsewhere in your code
      // close event for example
      Ti.APP.fireEvent('updatecitylabel', myCity);
      I have used Ti.App... as the event scope here as you are using the url window property, you should look to narrow the scope away from generic objects if possible, you can read more about that as above Mobile Best Practices: Applications shall run in a single JavaScript context

    — commented 10 months ago by Malcolm Hollingsworth

  • Nice this can also be done

    — commented 10 months ago by Umaid Saleem

1 Answer

Accepted Answer

Hi Umaid,

Try to use fireEvent because in your code you use this which point your modal window not parent window.

Ti.App.addEventListener('updateText',function(e){
    var mycity = Titanium.App.Properties.getString('City');
       cityLabel.text = mycity; 
});
fire this event from you modal window close event.
Ti.App.fireEvent('updateText');

Your Answer

Think you can help? Login to answer this question!