Search code examples
onsen-ui

Call function when a tab in tabbar is first opened (onsen-ui)


I need to load a specific JavaScript function after one of the tabs in the tabbar finishes loading (after it is clicked for the first time). I don't really understand how to set up an event listener with the onsen-ui tabbar (ons-tabbar), and the docs were not very clear.

Basically, what I am trying to do, is create a few graphs on one of the pages. This page is loaded up when a ons-tab is selected and the partial loads. However, I need the JavaScript that creates the graphs to only load after the page is loaded (the JavaScript looks for the elements to position the graph in the partial, hence my problem right now is that the JS is loading first and not finding the html elements in the partial because they have't been loaded yet). How do I create an event listener to detect when one of the tabs are selected to run a specific code of JavaScript?

To be a little bit more clear, I am using the persistent attribute on this specific tab, so essentially I only need this event listener to run the JS code once, when the tab is first opened. However, I don't understand how to get the once event listener to work...


Solution

  • For the reference: http://onsen.io/reference/ons-tabbar.html#methods-summary

    There are three tabbar methods related to events: on, once and off. That means the way to use them is myTabbar.on(...), for example. You can see in that link their parameters as well. Also, right below there are the three tabbar events. In this case I think you want to use postchange, right?

    In your HTML you create a tabbar and assign it to a variable with var attribute, as I am sure you already know:

    <ons-tabbar var="myTabbar">
        <ons-tab page="tab0.html" label="Tab0" active="true"></ons-tab>
        <ons-tab page="tab1.html" label="Tab1"></ons-tab>
        <ons-tab page="tab2.html" label="Tab2"></ons-tab>
    </ons-tabbar>
    

    Now, you have to wait until the app is initialised and Onsen UI is loaded in order to set the event listeners, and you can use ons.ready() for that.

    Going back to the tabbar events method, since you only need to trigger the functionality with a specific tab you need to filter the tabs inside the listener. Therefore, if you use myTabbar.once('postchange', ...) the listener itself will be triggered only once regardless the filter you set inside, so perhaps it will be triggered with a different tab and then deleted for the tab you want. You cannot use it in this case.

    Instead of that, you can set a listener with myTabbar.on('postchange', ...) that triggers always, then filter the tabs inside and remove the listener only when you want with myTabbar.off('postchange'). Something like this:

    ons.ready(function() {
    
      myTabbar.on('postchange', function(event) {
    
      // Only with the tab we want, we can use the index or the name
      if (event.index === 2) {
    
        console.log('postchange and do stuff with the tab');
        console.log(event.tabItem);
    
        // Delete this listener so it's only triggered the first time
        myTabbar.off('postchange');
      }
    
    });
    

    Working here: http://codepen.io/frankdiox/pen/QbYEaq

    In case you have other listener over postchange, you can use the second parameter of myTabbar.off(...) to remove the exact listener you want and not the others.

    Hope it helps!