2016-12-19 4 views
0

SAPUI5アプリケーションでoDataサービスを使用して新しいエントリを作成する簡単なフォームを作成します。私のアプローチは、以下のようなダイアログを作っている:SAPUI5のXMLビューのjsコントローラで生成されたダイアログを使用してフォームを送信

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core"> 
<Dialog id="CreateDialog" title="Add New Employee"> 
    <content> 
     <Button id="bt" text="s"></Button> 
      <sap.ui.layout.form:Form editable="true" xmlns:sap.ui.layout.form="sap.ui.layout.form" id="__form4"> 
      <sap.ui.layout.form:formContainers> 
       <sap.ui.layout.form:FormContainer title="Title" id="__container4"> 
        <sap.ui.layout.form:formElements> 
         <sap.ui.layout.form:FormElement label="Employee ID" id="__element5"> 
          <sap.ui.layout.form:fields> 
           <Input width="100%" id="txtEmpid" placeholder="Emp ID"/> 
          </sap.ui.layout.form:fields> 
         </sap.ui.layout.form:FormElement> 
        </sap.ui.layout.form:formElements> 
       </sap.ui.layout.form:FormContainer> 
      </sap.ui.layout.form:formContainers> 
      <sap.ui.layout.form:layout> 
       <sap.ui.layout.form:ResponsiveGridLayout id="__layout3"/> 
      </sap.ui.layout.form:layout> 
     </sap.ui.layout.form:Form> 
     <Button id="_btnSubmit" text="Submit" press="submitDialog"/> 
    </content> 
</Dialog> 

私はid ="txtEmpid"与えられたid="bt"入力テキストを与えられたボタンがある簡単なフォームを作成しました。私の問題は私のコントローラでこのIDに到達できないということです。これは私が試みた方法です:私はpress=submitDialogを与えるボタンを追加し、全体のビューを作成したコントローラ内のメソッド(idでコントロールを取得し、単にテキストを変更する)を実装します。

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller) { 
"use strict"; 
return Controller.extend("Demo1.controller.InitView", { 
showCreateDialog: function() { 
     var view = this.getView(); 
     var createDialog = view.byId("CreateDialog"); 
     var oDummyController = { 
// This is when I clicked the Submit button in dialog 
      submitDialog: function() { 
       var button = this.getView().byId("bt"); 
       var inputText = this.getView().byId("txtEmpid"); 
       // I tried to test if I reached these controls by byId method 
       button.setText("ssss"); 
       inputText.setValue("zzz"); 
      }, 
      closeDialog: function() { 
       createDialog.close(); 
      } 
     }; 
// This is when the dialog event is fired, things are fine here 
     if (!createDialog) { 
      createDialog = sap.ui.xmlfragment(view.getId(), "Demo1.view.CreateDialog", oDummyController); 
     } 
     view.addDependent(createDialog); 
     createDialog.open(); 
     if (!createDialog.isOpen()) { 
      //do sth 
     } 
    } 
}); 
}); 

私がコントロールに達するだけでなく、そのテキストを変更することはできませんどのようなhappern、これはErrorで見てみました。

任意の提案、

よろしく、

答えて

0

あなたはすでにあなたのコード内でこの問題を解決してきました。あなたはそれを見ることはできません。

あなたの "submitDialog"では、 "これ"はあなたが期待しているものを参照していません。すでに定義しているview.byId()でこれを行う場合は、おそらくうまくいくでしょう。なぜなら、あなたが "view"変数を定義している間に、同時に "this"という正しい参照を得るからです。通常、これを行うには推奨される方法ではありません。私たちのほとんどは、このような操作を行います。

var that = this; 

をそして

showCreateDialog: function() { 
    var that = this; 

    var oDummyController = { 
     submitDialog: function() { 
      var button = that.getView().byId("bt"); 
+0

おかげでたくさんのようにそれを使用します。あなたの方法は今私の新しいベストプラクティスです –

+1

あなたは大歓迎です。そして、Udacityの「Object Oriented Javascript」コースの「Scope」と「this keyword」の部分を見てみましょう。私はそれがあなたにとって非常に役に立つと確信しています。 –

関連する問題