UPDATE:
您可以使用DataItem的API與原型來管理您的數據。
不能將JSON對象直接綁定到模板,必須將它們轉換爲DataItem對象。檢索所需的節元素併爲節創建一個新的數據項。從JSON對象中創建新的數據項。 setPropertyPath方法用於將新數據項綁定到段數據項。清單5-4顯示了一個修改過的parseJson函數,該函數從Images.json文件獲取信息,將它們轉換爲數據項並將它們綁定到適當的部分。
使用原型元素,可以創建包含類似對象的單個鎖定。在鎖定的內部,您可以定義鎖定的結構。清單5-5顯示了一個鎖定,它將類型元素中定義爲圖片的對象顯示出來。每個圖像的URL和標題都從JSON對象中提取。
<prototypes>
<lockup prototype="artwork">
<img binding="@src:{url};" width="200" height="300"/>
<title binding="textContent:{title};" />
</lockup>
</prototypes>
<section binding="items:{images};" />
以下迭代和填充使用你的原型代碼items
從section
:
function parseJson(information) {
var results = JSON.parse(information);
let parsedTemplate = templateDocument()
navigationDocument.pushDocument(parsedTemplate)
let shelf = parsedTemplate.getElementsByTagName("shelf").item(0)
let section = shelf.getElementsByTagName("section").item(0)
//create an empty data item for the section
section.dataItem = new DataItem()
//create data items from objects
let newItems = results.map((result) => {
let objectItem = new DataItem(result.type, result.ID);
objectItem.url = result.url;
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)
}
模板:
<document>
<stackTemplate>
<banner>
<title>JSON Shelf</title>
</banner>
<collectionList>
<shelf>
<prototypes>
<lockup prototype="artwork">
<img binding="@src:{url};" width="200" height="300"/>
<title binding="textContent:{title};" />
</lockup>
</prototypes>
<section binding="items:{images};" />
</shelf>
</collectionList>
</stackTemplate>
</document>
參考:
https://developer.apple.com/library/content/documentation/TVMLKitJS/Conceptual/TVMLProgrammingGuide/GeneratingContentForYourApp.html
希望它可以幫助你。
非常感謝。這是事實,我可以使用原始的html與我缺少的insertAdjacentHTML()。 – ebr