Search code examples
tvostvml

Error in Apple's TVML documentation? pushPage function doesn't work


UPDATED 6/1/17 with the correct code pasted at the bottom.

I'm working through Apple's TVML guide, section 2: Navigating Between Pages. (https://developer.apple.com/library/content/documentation/TVMLKitJS/Conceptual/TVMLProgrammingGuide/NavigatingBetweenPages.html#//apple_ref/doc/uid/TP40016718-CH9-SW1)

Everything is fine until the last bit (Listing 4-4), which allow you to use the menu button on the remote to return to the previous page. Whenever I try it, my sample app simply won't load:

var baseURL;

function loadingTemplate() {
    var template = '<document><loadingTemplate><activityIndicator><text>Loading</text></activityIndicator></loadingTemplate></document>';
    var templateParser = new DOMParser();
    var parsedTemplate = templateParser.parseFromString(template, "application/xml");
    return parsedTemplate;
}

function getDocument(extension) {
    var templateXHR = new XMLHttpRequest();
    var url = baseURL + extension;
    var loadingScreen = loadingTemplate();

    templateXHR.responseType = "document";
    templateXHR.addEventListener("load", function() {pushPage(templateXHR.responseXML, loadingScreen);}, false);
    templateXHR.open("GET", url, true);
    templateXHR.send();
}

function pushPage(page, loading) {
var currentDoc = getActiveDocument();
    navigationDocument.replaceDocument(page, loading);
}

App.onLaunch = function(options) {
    baseURL = options.BASEURL;
    var extension = "templates/InitialPage.xml";
    getDocument(extension);
}

What am I missing?


This works:

var baseURL;

function loadingTemplate() {
    var template = '<document><loadingTemplate><activityIndicator><text>Loading</text></activityIndicator></loadingTemplate></document>';
    var templateParser = new DOMParser();
    var parsedTemplate = templateParser.parseFromString(template, "application/xml");
    navigationDocument.pushDocument(parsedTemplate);
    return parsedTemplate;

}

function getDocument(extension) {
    var templateXHR = new XMLHttpRequest();
    var url = baseURL + extension;
    var loadingScreen = loadingTemplate();

    templateXHR.responseType = "document";
    templateXHR.addEventListener("load", function() {pushPage(templateXHR.responseXML, loadingScreen);}, false);
    templateXHR.open("GET", url, true);
    templateXHR.send();
}

function pushPage(page, loading) {
    navigationDocument.replaceDocument(page, loading);
}

App.onLaunch = function(options) {
    baseURL = options.BASEURL;
    var extension = "templates/InitialPage.xml";
    getDocument(extension);
}

Solution

  • Yes, I believe there is a mistake. They should have kept the line

    navigationDocument.pushDocument(parsedTemplate);

    at the end of the loadingTemplate method.

    The idea is to push the loading page, then replace it with the new page.

    On a side note, the line

    var currentDoc = getActiveDocument();

    has no business here. This code was obviously not tested or reviewed.