How to check if a tab already exist?

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

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

— asked 11 months ago by von Goethe
0 Comments

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);

— answered 11 months ago by Dan Wilson
answer permalink
1 Comment
  • Finally, about time, that worked, Thank you very much Dan, not only with the tab thing, but also with my code, it helped me a lot, you have no idea how many hours I spend on that.

    I'm gonna leave the full code in here so other people that are having similar problems can use it, looking at it, it's a simple and elegant solution.

    var tabs = {};
    // function create channel
    function createChannel(e){
        var win = Ti.UI.createWindow({
            title: e.channelName,
            navBarHidden: false,
            channelId: e.channelId,
            channelName: e.channelName,
            channelPic: e.channelPic,
            url: 'chatUser.js'
        });
     
        var tab = Ti.UI.createTab({
            icon: 'KS_nav_ui.png',
            title: e.channelName,
            window: win
        });
     
        tabs[e.channelName] = tab;
     
        tabGroup.addTab(tab);
        setTimeout(function(){tabGroup.setActiveTab(tab)},500);
    } // end function createChannel
     
    Ti.App.addEventListener('createChannel',function(e){
        if (typeof tabs[e.channelName] == 'undefined') {
        createChannel(e);
        }
    });

    — commented 11 months ago by von Goethe

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.

— answered 11 months ago by Dan Wilson
answer permalink
3 Comments
  • Thanks for the answer man, but I couldn't get it to work, i tried it vanilla, with eval(a lot of variable not found) and last but not least.

    var tabs = {};
     
        var tab1 = Ti.UI.createTab({
        //icon    : myIcon,
        title   : e.channelName//,
        //window  : eval(win) // gives variable not found error
    });
     
    tabs['Main'] = tab1;
     
    if (typeof tabs['Main'] != 'undefined') {
        alert("exist");
       return;
    }

    — commented 11 months ago by von Goethe

  • You don't need to eval win. That doesn't really make much sense. Here's how I would rewrite your code.

    function createChannel(e) {
        var win = Ti.UI.createWindow({
            title: e.channelName,
            navBarHidden: false,
            channelId: e.channelId,
            channelName: e.channelName,
            channelPic: e.channelPic,
            url: 'chatUser.js'
        });
     
        var tab = Ti.UI.createTab({
            icon: 'KS_nav_ui.png',
            title: e.channelName,
            window: win
        });
     
        tabs[e.channelName] = tab;
    }
    Of course, you will need to initialize the tabs variable prior to calling the function.

    — commented 11 months ago by Dan Wilson

  • Thank you for the rewrite on the code, that works much better and actually correct a bug that sometimes the pun bun session would not load correctly.

    However when adding the function listed above, it always say the tab already exist.

    PS: Thanks for taking the time to help me out.

    function createChannel(e){
        var win = Ti.UI.createWindow({
            title: e.channelName,
            navBarHidden: false,
            channelId: e.channelId,
            channelName: e.channelName,
            channelPic: e.channelPic,
            url: 'chatUser.js'
        });
     
        var tab = Ti.UI.createTab({
            icon: 'KS_nav_ui.png',
            title: e.channelName,
            window: win
        });
     
        var tabs = {};
        tabs[e.channelName] = tab;
     
        if (typeof tabs[e.channelName] != 'undefined') {
        alert("exist");
       return;
    }
     
        tabGroup.addTab(tab);
        setTimeout(function(){tabGroup.setActiveTab(eval(tab))},500);
    }

    — commented 11 months ago by von Goethe

Your Answer

Think you can help? Login to answer this question!