2017-05-22 8 views
-1

上のjavascript関数への "この" コンテキストを渡します。 「this」は認識されず、ヌル値を持ちます。私はこれではない非常にエレガントなソリューションを使用するために選んました:私は<a href="https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.ODataModel.html" rel="nofollow noreferrer">ODataModel</a>オブジェクトのfuncionを以下しているsapui

var viewCurrent = this.getView(); 

serviceModel.read(
    "/Users(1)", { 
    success: function(userModel) { 
     viewCurrent.setModel(userModel, "userAuthenticated"); 
    }, 

    error: function(error) { 

    }, 

    context: this 
    } 
); 

とにかく、私は、パラメータとして「この」コンテキストを渡すための正しい方法でどのようにしたいと思います。

ありがとうございました。

答えて

0

read the documentationの場合、contextは機能コンテキストと相関していないことがわかります。

コンテキストを正しく設定するには、いくつかのオプションがあります。最初はarrow functionを使用して、thisを現在のスコープthisの値にバインドします。

serviceModel.read(
    "/Users(1)", { 
    success: (userModel) => { 
     this.getView().setModel(userModel, "userAuthenticated"); 
    }, 

... 

あなたは矢印の機能をサポートしていない環境で実行している場合、あなたは常にあなたが機能のthis内部の値を指定することを可能にするbindを使用することができます。

serviceModel.read(
    "/Users(1)", { 
    success: function(userModel) { 
     this.getView().setModel(userModel, "userAuthenticated"); 
    }.bind(this), 

... 
関連する問題

 関連する問題