【问题标题】:WebOS 3.0 Access to UI from functionWebOS 3.0 从函数访问 UI
【发布时间】:2017-02-20 10:55:09
【问题描述】:

我无法在 WebOS 3.0 中从 function:onComplete 访问 enyo Ui 组件。

buttonTapped: function(inSender, inEvent) {
    console.log("Button is clicked");
    this.$.txt.setContent(inSender.name + " tapped."); // This worked
    var request = webOS.service.request("luna://com.webos.service.tv.systemproperty", {
        method: "getSystemInfo",
        parameters: {"keys": ["modelName", "firmwareVersion", "UHD", "sdkVersion"]},
        onComplete: function (inResponse) {
            var isSucceeded = inResponse.returnValue;
            if (isSucceeded){
               console.log("Result: " + JSON.stringify(inResponse));
               $.txt.setContent("Result: "+JSON.stringify(inResponse)); // This is not worked
            }
        }
    });
...

控制台输出

按钮点击 结果{"modelName":"WEBOS1","firmwareVersion":"03.00.00","UHD":"false","sdkVersion":"03.00.00","re​​turnValue":true} 未捕获的类型错误:无法读取未定义的属性“txt”

我没有找到任何关于此的文档。

【问题讨论】:

    标签: javascript television webos enyo


    【解决方案1】:

    错误的原因是你的回调函数没有在你的组件上下文中执行。 this 不是您的组件(更不用说您在 $.txt... 前面缺少关键字 this)。

    您需要做的是绑定回调函数的上下文或使用它来创建一个包含对this 的引用的变量的闭包。

    这是一种常见的情况,Enyo 为此提供了一个实用方法:this.bindSafely

    尝试以下方法:

    onComplete: this.bindSafely(function (inResponse) {
        var isSucceeded = inResponse.returnValue;
        if (isSucceeded){
            console.log("Result: " + JSON.stringify(inResponse));
            this.$.txt.setContent("Result: "+JSON.stringify(inResponse));
        }
    })
    

    见:http://enyojs.com/docs/latest/#/kind/enyo/CoreObject/Object:bindSafely

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多