how to pass parameters from tableview to another view?
Newb question…
I am trying to create a custom phone number picker.
I am able to pull the FULLNAMES out of the contactlist, and createTableView.
I am also able to pull the individual numbers out of the Phone array.
I am looking at the example: contacts_db.js, but I don't want to display the address. I have some other buttons and stuff I would want to add.
IDEALLY, I want to create a new .js file that will display the phone numbers with buttons.
MY QUESTION is…
how do I pass the ROWNUMBER of the TableView on click to another .js file as a parameter?
If I get the row number, than I have the contact number, and I can render the view as I want. Besides, I want to use this view on another page.
I was also thinking of setting the number in the click event:
Titanium.App.Properties.setInt('contactnumber',i); // use this to build view of contacts
can I get a hint please..
5 Answers
-
This doesn't seem to work for me… How can I access the "moo" object in my second view which is generated (in the example) via "bar.js"?
-
the easiest way, and i think the best architecture is to fire events passing the information you want and then listening for these events in the other js files.
so in you table view click event
Ti.App.fireEvent.fireEvent('user_selected', { // you can also just pass the whole object here... row_id : 10; });
now anywhere in you application, listen for this event and you will get the data
someDetailedView.addEventListener('user_selected', function(data) { Ti.API.info("user_selected " + data. row_id); // get the data from the data store });
-
Hey, if i understand you correctly then the answer lies in the API documentation at the bottom of the UI.Window page - Passing Data.
Basically since data in one script cant access data or functions from another (because they run in seperate contexts) what you need to do is pass your variable/method to the script you want.
Here's an example:
var foo = "test" var win = Titanium.UI.createWindow({ url:'bar.js' }); win.moo = foo;
Notice the last line - this is basically saying "add a variable 'moo' to the 'win' object and let it be the value of a variable 'foo' in the current script"
Within your bar.js script you would now be able to make use of the variable 'moo' and it will have the value that 'test' had in the previous window.
-
is that…
win.moo = test;or
win.moo = foo;
that may have been a typo?
but thanks for the explanation.
-
My code..
customers.js :
//create tableView event listener tableView.addEventListener('click', function(e) { //set properties for passed info var newWin = Titanium.UI.createWindow({ title:e.row.name, url:'customerDetail.js', passedName:e.row.name, passedID:e.row.id }); newWin.open({animated:true, modal:true}); });
customerDetail.js:
var custName = win.passedName;
then custName will hold the value of whichever row was clicked in customers.js