Search code examples
tvostvmltvjs

How to handle tvOS MenuBarTemplate selection?


I have a basic MenuBarTemplate set up and displaying.

How do I react to a user's Menu selection and load an appropriate content template?


Solution

  • In the menuItem tag include a template attribute pointing to the template to load and a presentation attribute set to menuBarItemPresenter.

    <menuItem template="${this.BASEURL}templates/Explore.xml.js" 
      presentation="menuBarItemPresenter">
        <title>Explore</title>
    </menuItem>
    

    You can then use the menu bar's MenuBarDocument feature to associate a document to each menu bar item.

    menuBarItemPresenter: function(xml, ele) {
      var feature = ele.parentNode.getFeature("MenuBarDocument");
      if (feature) {
        var currentDoc = feature.getDocument(ele);
        if (!currentDoc) {
          feature.setDocument(xml, ele);
        }
      }
    

    This assumes you're using a Presenter.js file like the one in Apple's "TVML Catalog" sample. The load function specified there is what calls the function specified in the menuItem's presentation attribute.