Good evening guys, I'm creating tabs dynamically for a chat component of my app, each new conversation will open in a new tab, however, how can I check if a tab already exist in order not to create multiple tabs with the same content?
i tried typeof, tried: var jhr = "tabGroup."+tab; if (eval(jhr)) alert("something");
I also tried the option listed in the question below but changing from view to tab and adding tab group instead of window, but nothing. http://developer.appcelerator.com/question/130728/how-to-check-if-view-exists
I thought about doing a tabGroup.getTabs() and then a for search inside the array, but that sounded like overkill.
function createChannel(e){ var tab = "tab"+e.channelId; // create window dynamically eval("win"+e.channelId+" = Titanium.UI.createWindow({"+ "title:'"+e.channelName+"',"+ "navBarHidden: false,"+ "channelId:'"+e.channelId+"',"+ "channelName:'"+e.channelName+"',"+ "channelPic:'"+e.channelPic+"',"+ "url:'chatUser.js'"+ "});"); // create tab dynamically eval(tab+" = Titanium.UI.createTab({"+ "icon:'KS_nav_ui.png',"+ "title:'"+e.channelName+"',"+ "window:win"+e.channelId+ "});"); tabGroup.addTab(eval(tab)); setTimeout(function(){tabGroup.setActiveTab(eval(tab))},500); } // end function createChannel
2 Answers
Accepted Answer
Wrap the call to the function in the if statement that checks if it exists:
if (typeof tabs[e.channelName] == 'undefined') { createChannel(e); }And then take the if statement out of the function.
Also, you don't need the eval around the tab variable within the setTimeout:
setTimeout(function(){tabGroup.setActiveTab(tab)},500);
I do this by managing an object/array containing the tab objects:
var tabs = {}; var tab = Ti.UI.createTab({ icon : myIcon, title : 'Main', window : tabwin }); tabs['Main'] = tab;Then I can check if the tab exists as a property within "tabs": (this code untested)
if (typeof tabs['Main'] != 'undefined') { // tab "Main" already exists }You don't need to do all the eval's with this code. The one variable contains everything you need. No need to eval a specifically named window or tab variable.
Your Answer
Think you can help? Login to answer this question!