Skip to content

Instantly share code, notes, and snippets.

@mlcollard
Created April 30, 2018 14:38
Show Gist options
  • Save mlcollard/9cdb26244e09add94babf44885c9e9c9 to your computer and use it in GitHub Desktop.
Save mlcollard/9cdb26244e09add94babf44885c9e9c9 to your computer and use it in GitHub Desktop.
iOS: TVML: Load data from Json file and display image
var baseURL = "http://localhost:9001/"
function getDocument(url) {
var templateXHR = new XMLHttpRequest();
templateXHR.responseType = "document";
templateXHR.addEventListener("load", function() { pushDoc(templateXHR.responseXML);}, false);
templateXHR.open("GET", baseURL + url, true);
templateXHR.send();
}
function getDocumentJson(url) {
var templateXHR = new XMLHttpRequest();
templateXHR.responseType = "document";
templateXHR.addEventListener("load", function() { parseJson(templateXHR.responseText);}, false);
templateXHR.open("GET", baseURL + url, true);
templateXHR.send();
}
function pushDoc(document) {
navigationDocument.pushDocument(document);
}
function loadingDoc() {
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);
}
function parseJson(information) {
var result = JSON.parse(information);
// list of mascots
var mascots = "";
for (i = 0; i < result.length; ++i) {
mascots += '<lockup><img src="' + baseURL + result[i].url + '" width="182" height="274" /><title>' + result[i].title + '</title></lockup>';
}
// page of mascots
var template = '<document><stackTemplate><banner><title>Mascots</title></banner><collectionList><shelf><section>' + mascots + '</section></shelf></collectionList></stackTemplate></document>';
// construct page
var templateParser = new DOMParser();
var parsedTemplate = templateParser.parseFromString(template, "application/xml");
// view
navigationDocument.pushDocument(parsedTemplate);
}
App.onLaunch = function(options) {
loadingDoc()
var templateURL = "images.json";
getDocumentJson(templateURL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment