Skip to Content
0

How Can I use global variable in model?

Nov 29, 2016 at 03:59 PM

15

avatar image
Former Member

Here is the controller code. I want to move functions below to a model. But the problem is all the variables are declared a global variable and used in function called globalEnv. I am very new to this framwork not sure how I can achieve this.

I wanted something like where I can create a model like formatter.js and move code to that file

sap.ui.define([], function () {
    "use strict";

    return {

    
        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");
            }


        },

        quantityFormatCheck: function(qcheck) {
            var detailId = oEvent.getParameters().id;
            var id = detailId.replace(/\__xmlview0--\b/, "");
            var inputVal = qcheck.getParameters().value;

            if (id === "priorAwardQuantInput") {

                if (!inputVal.match(validQuantity) && inputVal !== "") {
                    awardQuantity.setValueState("Warning");
                    awardQuantity.setValueStateText("Invalid Entry");
                } else {
                    awardQuantity.setValueState("None")
                };
            } else if (id === "currentBuyQuantInput") {

                if (!inputVal.match(validQuantity) && inputVal !== "") {
                    buyQuantity.setValueState("Warning");
                    buyQuantity.setValueStateText("Invalid Entry");
                } else {
                    buyQuantity.setValueState("None")
                };
            }
        },


    };
});
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 nsnButton, pdfButton, nsnSearchInput, searchResultTable, priceEvalGridContainer, nsnOutput, searchTableBinding, validCharacters, validCurrency, validQuantity, validLearningCurve, awardQuantity, awardDate, unitPrice, buyQuantity, commentBoxContainer, learningCurve, price, missingFieldError, invalidFieldError, searchQuery, iQueryLength;

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


            //define global variables
            globalEnv: function () {
                nsnButton = this.byId("nsnButton");
               
                searchResultTable = this.byId("searchResultsTable");
                priceEvalGridContainer = this.byId("priceEvalGridContainer");
                searchTableBinding = searchResultTable.getBinding("items");
                validCharacters = /^[0-9a-zA-Z]+$/;
                validCurrency = /^[\s\d$.,]+$/;
                awardQuantity = this.byId("priorAwardQuantInput");
             
                price = this.byId("price");
                awardDate = this.byId("priorAwardDateInput");
                missingFieldError = this.byId("missingFieldError");
                invalidFieldError = this.byId("invalidFieldError");
                searchQuery = nsnSearchInput.getValue();
                iQueryLength = searchQuery;
            },

            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();
                priceEvalGridContainer.setVisible(false);
            },

            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");
                }


            },


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

                if (id === "priorAwardQuantInput") {

                    if (!inputVal.match(validQuantity) && inputVal !== "") {
                        awardQuantity.setValueState("Warning");
                        awardQuantity.setValueStateText("Invalid Entry");
                    } else {
                        awardQuantity.setValueState("None")
                    };
                } else if (id === "currentBuyQuantInput") {

                    if (!inputVal.match(validQuantity) && inputVal !== "") {
                        buyQuantity.setValueState("Warning");
                        buyQuantity.setValueStateText("Invalid Entry");
                    } else {
                        buyQuantity.setValueState("None")
                    };
                }
            },

            });

    return mainController;
});
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers