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;
});
그런 전역 변수를 사용하지 마십시오! 왜 그들을 필요로합니까? – matbtt