TVML listItemLockup点击事件

Ale*_*ley 1 tvos tvml

我正在使用TVMLCatalog中的' Compilation.xml '模板

我想将一个按钮点击事件添加到' listItemLockup '

<listItemLockup>
  <ordinal minLength="2" class="ordinalLayout">0</ordinal>
  <title>Intro</title>
  <subtitle>00</subtitle>
  <decorationLabel>(3:42)</decorationLabel>
</listItemLockup>
Run Code Online (Sandbox Code Playgroud)

我试过添加:

App.onLaunch = function(options) {
    var templateURL = 'http://localhost:8000/hello.tvml';
    var doc = getDocument(templateURL);
    //doc.addEventListener("select", function() { alert("CLICK!") }, false);
    var listItemLockupElement = doc.getElementsByTagName("listItemLockup");
    listItemLockupElement.addEventListener("select", function() { alert("CLICK!") }, false);
}
Run Code Online (Sandbox Code Playgroud)

的addEventListener

void addEventListener (in String type, in Object listener, in optional Object extraInfo)
Run Code Online (Sandbox Code Playgroud)

"选择"正确的类型?

我一直在使用以下教程

http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-with-swift/

http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-part-2/


更新

我收到了一个错误

ITML <Error>: doc.getElementsByTagName is not a function. (In 'doc.getElementsByTagName("listItemLockup")', 'doc.getElementsByTagName' is undefined) - http://localhost:8000/main.js - line:27:58
Run Code Online (Sandbox Code Playgroud)

我尝试将其添加到'onLaunch'

var listItemLockupElements = doc.getElementsByTagName("listItemLockup");
for (var i = 0; i < listItemLockupElements.length; i++) {   
    //var ele = listItemLockupElements[i].firstChild.nodeValue;
    listItemLockupElements[i].addEventListener("select", function() { alert("CLICK!") }, false);
}
Run Code Online (Sandbox Code Playgroud)

我先看看错误


Cross Post:https://forums.developer.apple.com/thread/17859

Ser*_*top 5

我见过的更常见的例子是定义一个整体监听器,如:

doc.addEventListener("select", Presenter.load.bind(Presenter));
Run Code Online (Sandbox Code Playgroud)

在xml中,为元素指定唯一ID,或者为它们提供识别元素的方法.例如,开头将是这样的:

load: function(event) {
        var self = this,
            ele = event.target,
            attr_id = ele.getAttribute("id"),
            audioURL = ele.getAttribute("audioURL"),
            videoURL = ele.getAttribute("videoURL")
Run Code Online (Sandbox Code Playgroud)

然后你可以用你的物品做任何你想做的事情.

if(audioURL && (event.type === "select" || event.type === "play")) {
//
}
Run Code Online (Sandbox Code Playgroud)

我的建议是更仔细地研究这个模式的Presenter.js文件.

编辑:回答与doc.getElementsByTagName相关的"更新"不是一个函数."doc"实际上并不存在,但一般的模式是用它来实现

var doc = getActiveDocument();
Run Code Online (Sandbox Code Playgroud)

我以为你会知道上面的内容.这样可以解决吗?