2016-11-29 3 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 
    ... 
} 

그런 다음 당신은 당신의 MainController에서 다음을 수행 할 수 있습니다 귀하의 컨트롤러 파일의.

관련 문제