我正在我的TVOS应用程序中创建一个页面,该页面根据2个不同的类别("完整现场集"和"现场单曲")解析我的json。我正在使用目录模板将类别放在左侧,将结果放在右侧。
使用以下代码,第一个类别按预期加载,因为它正在抓取第一个网格。我将如何将其扩展到第二个类别(最多 10 个左右的类别?
function getJSONDocument(url, category) {
var templateXHR = new XMLHttpRequest();
var fullURL = baseURL + url;
var loadingScreen = loadingTemplate();
templateXHR.responseType = "document";
templateXHR.addEventListener("load", function() {parseJson(templateXHR.responseText);}, false);
templateXHR.open("GET", fullURL, true);
templateXHR.send();
}
function parseJson(information) {
var results = JSON.parse(information);
let parsedTemplate = templateDocument()
navigationDocument.pushDocument(parsedTemplate)
//this works for templateDocument, but can only do one category / section / grid
let grid = parsedTemplate.getElementsByTagName("grid").item(0)
let section = grid.getElementsByTagName("section").item(0)
//this doesn't work parsedTemplate.getElementsByTagName("grid").item(1)
// doesn't work grid.getElementsByTagName("section").item(1)
//create an empty data item for the section
section.dataItem = new DataItem()
//create data items from objects
let newItems = results.map((result) => {
// this function requires each JSON entry to have an ID and a type
// the type is chosen in the xml in <lockup prototype="type">.
// should be set to whatever type you want to pull
let objectItem = new DataItem(result.type, result.ID);
objectItem.thumb = baseURL + result.thumb;
objectItem.title = result.title;
return objectItem;
});
//add the data items to the section's data item; 'images' relates to the binding name in the protoype where items:{images} is all of the newItems being added to the sections' data item;
section.dataItem.setPropertyPath("images", newItems)
}
function templateDocument() {
// to allow for different pages, this function needs to be called
// by passing a type with this function (templateDocument(livemusic))
let template = `<?xml version="1.0" encoding="UTF-8" ?>
<document>
<catalogTemplate>
<banner>
<title>Live Music</title>
</banner>
<list>
<section>
<listItemLockup>
<title>JSON</title>
<decorationLabel>heh</decorationLabel>
<relatedContent>
<grid>
<prototypes>
<lockup prototype="live single">
<img binding="@src:{thumb};" width="250" height="376" />
<title binding="textContent:{title};" />
</lockup>
</prototypes>
<section binding="items:{images};" />
</grid>
</relatedContent>
</listItemLockup>
<listItemLockup>
<title>JSON</title>
<decorationLabel>heh</decorationLabel>
<relatedContent>
<grid>
<prototypes>
<lockup prototype="full live set">
<img binding="@src:{thumb};" width="250" height="376" />
<title binding="textContent:{title};" />
</lockup>
</prototypes>
<section binding="items:{images};" />
</grid>
</relatedContent>
</listItemLockup>
</section>
</list>
</catalogTemplate>
</document>`;
return new DOMParser().parseFromString(template, "application/xml");
}
function parseJson(information) {
var results = JSON.parse(information);
let parsedTemplate = templateDocument()
navigationDocument.pushDocument(parsedTemplate)
//this works for templateDocument, but can only do one category / section / grid
let liveFullGrid = parsedTemplate.getElementsByTagName("grid").item(1)
//the array counting here starts again with 0 because its counting from inside grid #1
let liveFullSection = liveFullGrid.getElementsByTagName("section").item(0)
let liveSingleGrid = parsedTemplate.getElementsByTagName("grid").item(0)
let liveSingleSection = liveSingleGrid.getElementsByTagName("section").item(0)
//create an empty data item for the section
liveFullSection.dataItem = new DataItem()
liveSingleSection.dataItem = new DataItem()
//liveSingleSection.dataItem = new DataItem()
//create data items from objects
let newItems = results.map((result) => {
// this function requires each JSON entry to have an ID and a type
// the type is chosen in the xml in <lockup prototype="type">.
// should be set to whatever type you want to pull
let objectItem = new DataItem(result.type, result.ID);
objectItem.thumb = baseURL + result.thumb;
objectItem.title = result.title;
return objectItem;
});
//add the data items to the section's data item; 'images' relates to the binding name in the protoype where items:{images} is all of the newItems being added to the sections' data item;
liveFullSection.dataItem.setPropertyPath("images", newItems)
liveSingleSection.dataItem.setPropertyPath("images", newItems)
}