How do you get the value from a TVML textField?

2019-07-08 02:49发布

I am trying to figure out how to get the value from the textField in TVML using TVJS but I haven't found any documentation on it yet and I don't want to wait around for the documentation to be updated. There isn't really any patterns for binding to input since I'm sure this is a seldom use case other than logging into a service. I guess I could parse the Xml but that seems super hacky.

标签: apple-tv
2条回答
叼着烟拽天下
2楼-- · 2019-07-08 03:26

this solution doesn't work for me by some reason, but I found another one:

 var sender = event.target;
 var formTemplate = sender.parentNode.parentNode;
 var children = formTemplate.childNodes;
 var textField = children.item(1); // replace "1" with the index of "textField" element in your template                                                                                        
 var inputValue = textField.getFeature("Keyboard").text;
查看更多
祖国的老花朵
3楼-- · 2019-07-08 03:36

I ended up figuring it out myself. I had to use the callback from the select event object and then pass the text element into a function to handle the event.

function template() {
    var template = "<document><formTemplate><textField id="email">Your email</textField><footer><button><text>Submit</text></button></footer></formTemplate></document>";
    var parser = new DOMParser();
    var doc = parser.parseFromString(template , "application/xml");
    var email = doc.getElementById("email");
    doc.addEventListener("select", function() { submit(email); });
}

function submit(textField) {
    var keyboard textField.getFeature('Keyboard');
    var email = keyboard.text;
}

Once I had the node I used the apple TVJS function getFeature('Keyboard') to get the text value from this object.

查看更多
登录 后发表回答