簡體   English   中英

如何在控制器中聲明的模型中訪問變量?

[英]How can I access variables in model declare in a controller?

這是我擁有的控制器,我想將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;
});

不使用全局變量怎么樣? 您可以將變量設置為局部變量,並將它們作為參數傳遞給任何其他方法,即使在其他類中也是如此。

在您的utility.js定義您的傳輸方法,如下所示:

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

然后,您可以在MainController中執行以下操作:

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

當然,您必須在控制器文件的開頭導入實用程序類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM