Hi guys, I'm here asking for your help because I'm trying to write my first openDialog. The openDialog allow you to select your continent, country and city and the process go on four steps.
Steps:
1 - Select your continent
2 - new openDialog -> Select your country
3 - new openDialog -> Select your city
4 - return your choice
At the momoment I'm able to populate, create and open all openDialogs but I'm not able to return the choice.
Here is my code:
myapp.js
// on button click stat openDialog $.settingButton.addEventListener('click',function(e){ var GetWorld = require('JS/onWorld'); var getworld = new GetWorld(); });world.js
var setting = []; function GetWorld() { //inizializza setting setting.length=0; setting.push({title:'continent', value:'', status:false}); setting.push({title:'country', value:'', status:false}); setting.push({title:'city', value:'', status:false}); if(OnWorldSetting()) return setting; }; module.exports = GetWorld; function OnWorldSetting() { for(var i = 0; i < setting.length; i++) { if(!setting[i].status) { switch (i) { case 0: GetContinets(); return; case 1: GetCountry(); return; case 2: GetCity(); return; } } } if( setting[0].status, setting[1].status, setting[2].status) return true; } function GetContinets() { var regionArea = ["Africa", "Asia", "Europe", "North America", "Oceania", "South America", "Antarctica"]; OpenPopUp(regionArea, setting[0]); }; function GetCountry() { var xhr = Titanium.Network.createHTTPClient(); xhr.setTimeout([10000]); xhr.onload = function() { var regionArea = []; var json = JSON.parse(this.responseText); for(var i = 0; i < json.length; i++) { var jsnrows = json[i]; regionArea.push(jsnrows.Name); } OpenPopUp(regionArea, setting[1]); }; xhr.open("GET", linkdb+"world/country.php"); var params = { continent:setting[0].value }; xhr.send(params); }; function GetCity() { var xhr = Titanium.Network.createHTTPClient(); xhr.setTimeout([10000]); xhr.onload = function() { var regionArea = []; var json = JSON.parse(this.responseText); for(var i = 0; i < json.length; i++) { if(json[i].Name !='') regionArea.push(json[i].Name); } OpenPopUp(regionArea, setting[2]); }; xhr.open("GET", linkdb+"world/city.php"); var params = { state:setting[1].value }; xhr.send(params); }; function OpenPopUp(regionArea, imp) { var data = []; var view = Ti.UI.createView({ backgroundColor : '#212429' }); var tableView = Ti.UI.createTableView({ separatorColor : '#111214' }); for(var i=0; i < regionArea.length; i++){ var Row = Ti.UI.createTableViewRow({ touchEnabled : false, height : '50dp', width : '100%', mytext: regionArea[i], backgroundColor : '#212429' }); var label = Ti.UI.createLabel({ text : regionArea[i], color : 'white', touchEnabled : false, font : {fontSize : '18dp'} }); Row.add(label); data.push(Row); } view.add(tableView); tableView.setData(data); var dialog = Ti.UI.createOptionDialog({ title:"Select your " + set.title + ":", androidView:view, destructive: 0 }); tableView.addEventListener('click',function(e) { imp.value = e.rowData.mytext; imp.status = true; dialog.hide(); OnWorldSetting(); }); dialog.show(); }
var setting is the array where i store the user's choice.
GetContinets() , GetCountry() and GetCity let me populate the array.
OpenPopUp(regionArea, imp) let me open the pop up window.
Have you got any advice about my code? How can i Improve it? How can I return the user choice?
I dont know why but I can't open the openDialog on the first click, I need to click on "$.settingButton" twice.Why?
Regards, Matteo