2016-11-29 8 views
0

これは私が持っているコントローラーで、私はcurrencyCheckをコントローラーでロードできるモデルまたは別のutility.jsファイルに移動したいが、問題はグローバル変数である。グローバル変数を使って関数を別のjsファイルに移動する方法はわかりません。 UI5でグローバル変数を宣言する方法はありますか?モデル内の変数にコントローラで宣言するにはどうしたらいいですか?

sap.ui.define([ 
    'jquery.sap.global', 
    'sap/ui/core/mvc/Controller', 
    'sap/ui/model/json/JSONModel', 
    'sap/ui/model/Filter', 
    'sap/ui/model/FilterOperator', 
    'sap/m/MessageToast' 
], 

function(jQuery, Controller, JSONModel, Filter, FilterOperator, MessageToast) { 
    "use strict"; 

    var price; 
    var mainController = Controller.extend("pricingTool.controller.Main", { 


     //define global variables 
     globalEnv: function() { 
      nsnButton = this.byId("nsnButton"); 
      price = this.byId("price"); 
     }, 

     onInit: function(oEvent) { 

      //moving this code to Component.js 
      //define named/default model(s) 
      var inputModel = new JSONModel("model/inputs.json"); 
      var productsModel = new JSONModel("model/products.json"); 

      //set model(s) to current xml view 
      this.getView().setModel(inputModel, "inputModel"); 
      this.getView().setModel(productsModel); 

      //default application settings 
      //unload global variables 
      this.globalEnv(); 
     }, 

     currencyCheck: function(oEvent) { 
      var inputVal = oEvent.getParameters().value; 
      var detailId = oEvent.getParameters().id; 
      var id = detailId.replace(/\__xmlview0--\b/, ""); 
      var currencyCode; 
      var inputArr = inputVal.split(""); 

      currencyCode = inputArr[0] + inputArr[1] + inputArr[2]; 

      if (id === "price") { 

       if (inputArr[0].match(/^[\d$]+$/) || currencyCode === 'USD') { 
        price.setValueState("None"); 
       } else price.setValueState("Error"); 


      } else if (id === "unitPrice") { 
       console.log(inputVal); 
       if (inputArr[0].match(/^[\d$]+$/) || currencyCode === 'USD') { 
        unitPrice.setValueState("None"); 
       } else unitPrice.setValueState("Error"); 
      } 


     }, 

     onNsnChange: function() { 
      //enable "Search" button if input has an entry 
      searchQuery = nsnSearchInput.getValue(); 

      if (searchQuery === "") { 
       nsnButton.setEnabled(false); 
      } else { 
       nsnSearchInput.setValueState("None"); 
       nsnButton.setEnabled(true); 
      } 
     }, 


    }); 

    return mainController; 
}); 
+0

このようなグローバル変数は使用しないでください。なぜあなたはそれらを必要としますか? – matbtt

答えて

2

グローバル変数を使用しないでください。変数をローカルにして、他のクラスの他のメソッドにもパラメータとして渡すことができます。で

あなたutility.js次のようにあなたの転送方法を定義します。あなたが最初にあなたのユーティリティクラスをインポートする必要がもちろん

currencyCheck: function (oEvent) { 
    var oPrice = this.byId("price"); 
    Utility.currencyCheck(oEvent, oPrice); 
} 

currencyCheck: function (oEvent, price) { 
    ... 
    // the code from the original function 
    ... 
} 

は、その後、あなたのメインコントローラで次の操作を行うことができますあなたのコントローラファイルの。

関連する問題