2017-02-20 11 views
0

WebOS 3.0の関数:onCompleteからenyo Uiコンポーネントにアクセスできません。WebOS 3.0関数からの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 
      } 
     } 
    }); 
... 

コンソール出力

 
Button clicked 
Result{"modelName":"WEBOS1","firmwareVersion":"03.00.00","UHD":"false","sdkVersion":"03.00.00","returnValue":true} 
Uncaught TypeError: Cannot read property 'txt' of undefined 

私はこれについての文書が見つかりません。

答えて

1

エラーの原因は、コールバック関数がコンポーネントのコンテキストで実行されていないためです。 thisはあなたのコンポーネントではありません(キーワードthis$.txt...の前にありません)。

あなたがしなければならないことは、コールバック関数のコンテキストをバインドするか、または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

関連する問題