Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Passing Data Between Window?.

I want send data to one window from other. The main window has a table view. while clicking on the table view, I want to pass corresponding id to next window. The window is in other .js file. Presently, I am passing data by dispatching a custom event. But first time it doesn't work. Please help me.

tableView.js code..

var tableView=Titanium.UI.createTableView({data:data});

var nav = Titanium.UI.iPhone.createNavigationGroup({
window: win1
});

var showWin = Titanium.UI.createWindow({
url:"files/showPaper.js",
title:'Info'
});

tableView.addEventListener('click', function(e)
{
Titanium.API.info("sdfsdfsdF"+e.rowData.val);

Titanium.API.info("hjhjhjhjhjhjhjhjh");
//showWin.fireEvent('createTable',{id:e.rowData.val});
nav.open(showWin,{animated:true});
showWin.fireEvent('createTable',{id:e.rowData.val});

});
win1.add(tableView);
mainWin.add(nav);

showWin.js

var showWin=Titanium.UI.currentWindow;
//showWin.title= "jkjkjkjk";
Titanium.API.info("in Show Creation");
var itemId;

showWin.addEventListener('createTable',function(e)
{
Titanium.API.info("in Show Event");
itemId=e.id;
createTable();
});

I think, first time the eventListener not Registered.

— asked December 7th 2010 by sanal ms
0 Comments

2 Answers

  • Accepted Answer

    What I do instead of firing an event, is first create the new window, then add a custom property to it with the value I want, then open that window. In the new window, I just reference that property to find the value.

    tableView.addEventListener('click', function(e) { 
      Titanium.API.info("sdfsdfsdF"+e.rowData.val);
      showWin.myID = e.rowData.val;
      nav.open(showWin,{animated:true});
    }
    
    in showWin.js:
    
    Ti.API.info('myID = ' + showWin.myID);
    

    Personally, I wouldn't bother to create the showWin window until inside the table event listener. But I was trying to keep it as close to your code as possible.

    — answered December 7th 2010 by Doug Handy
    permalink
    0 Comments
  • This is how I would do it:

    Here is a simple example on how to pass and retrieve data between windows, I do the following:

    var win1 = Titanium.UI.createWindow({  
        title: 'My Main Window',
        backgroundImage: 'images/bg.png',
        MyID: e.rowData.val
    });
    win1.open();
    
    // On Win1 you do the following to retrieve
    // Declare your current window
    var win = Ti.UI.currentWindow;
    
    //RETRIEVING PARAMETERS FROM PREVIOUS WINDOW
    var MyID = win.MyID;
    

    This way you are passing to the next window the variable MyID.

    Hope it helped.

    — answered December 7th 2010 by Antonio Silveira
    permalink
    2 Comments
    • I found that the passed value can't be changed.
      e.g.

      var win1 = Titanium.UI.createWindow({  
          title: 'My Main Window',
          backgroundImage: 'images/bg.png',
          MyID: e.rowData.val
      });
      win1.open();
      
      // RETRIEVING PARAMETERS FROM PREVIOUS WINDOW
      var win = Ti.UI.currentWindow;
      var MyID = win.MyID;
      
      // TRACE THE VALUE
      Ti.API.debug(win.MyID); // e.g. it is 1
      
      // TRY TO CHANGE THE VALUE
      win.MyID = 2;
      
      // BUT IT STILL BE 1
      Ti.API.debug(win.MyID);
      

      Anybody knows why?

      — commented October 18th 2011 by Michael Chau
    • If the code is as below, it works:

      // TRY TO CHANGE THE VALUE
      win = {MyID: 2};
      

      It created a new object to replace old one, but why?

      — commented October 18th 2011 by Michael Chau
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.