Skip to Content
0

Set the value of a global variable in a js function

Apr 30, 2017 at 05:14 PM

78

avatar image

Hey everybody,

i am currently working on a sap fiori app that consumes an odata service.

I created a method in my controller "fonct" that calculates a variable coming from my odata.

I want to capture this value & put it in a global variable everytime the view is refreshed.

I created a global variable like "

var boo1;

return Controller.extend("com.controller.Detail", {...}

" and i passed it as a parameter in my method "fonct" inside my init method but it is undefined.

Here's my controller's code :

sap.ui.define([
    "com/util/Controller",
    "com/util/Formatter",
    "sap/ui/Device",
    "sap/m/MessageBox",
    "sap/m/MessageToast",
    "sap/ui/core/Fragment",
    "sap/m/MessagePopover",
    "sap/m/MessagePopoverItem",
    "sap/m/Link",
    "sap/ui/model/json/JSONModel"
], function(Controller, Formatter, Device, Fragment, MessageBox, MessageToast, MessagePopover, MessagePopoverItem, Link, JSONModel) {
    "use strict";
   var boo1;
    return Controller.extend("com.controller.Detail", {
        /**
         * Called when the detail list controller is instantiated. 
         */
        onInit: function() {
            this.oInitialLoadFinishedDeferred = jQuery.Deferred();
            this._bMessageOpen = false;
            this._sErrorText = this.getResourceBundle().getText("errorText");

            if (!this._detailReadMode) {
                this._detailReadModeFragmentName = this.getView().getId() + "detailReadMode";
                this._detailReadMode = sap.ui.xmlfragment(this._detailReadModeFragmentName, "com.view.DetailReadMode", this);
                this.switchMode("read");
            }

            if (!this._detailEditMode) {
                this._detailEditModeFragmentName = this.getView().getId() + "detailEditMode";
                this._detailEditMode = sap.ui.xmlfragment(this._detailEditModeFragmentName, "com.view.DetailEditMode", this);
            }

            if (!this._detailCreateMode) {
                this._detailCreateModeFragmentName = this.getView().getId() + "detailCreateMode";
                this._detailCreateMode = sap.ui.xmlfragment(this._detailCreateModeFragmentName, "com.view.DetailCreateMode", this);
            }

            if (Device.system.phone) {
                //don't wait for the master on a phone
                this.oInitialLoadFinishedDeferred.resolve();
            } else {
                this.getView().setBusy(true);
                this.getEventBus().subscribe("Master", "InitialLoadFinished", this.onMasterLoaded, this);
            }

            this.getRouter().attachRouteMatched(this.onRouteMatched, this);
            var that = this;
            that.fonct(boo1);
            alert(boo1);
            },          

            fonct : function(ovar){
            var that = this;
            var oModel = that.getView().getModel();
                oModel.read("/alertSet", {
                   method:"GET",
                   success: function(data){
                         var a = JSON.stringify(data);
                         var b = a.slice(332,-4);
                         ovar = b;
                   }, 
                   error: function(){
                      alert("error");
                   }});
                },   

Any help would be appreciated

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Sharath M G Apr 30, 2017 at 10:25 PM
0

1. bool isn't defined. Try car bool = false;

Regards,

Sharath

Share
10 |10000 characters needed characters left characters exceeded
Akhilesh Upadhyay May 01, 2017 at 08:21 AM
0

i think variable declaration and place is not correct,

try to put that variable inside controller.extend OR inside onInit function using "this" keyword as below:

return Controller.extend("com.controller.Detail", {/*** Called when the detaillist controller is instantiated.*/
        
boo1:"" // without "var" or inside onInit() as below
onInit:function(){
this.boo1 = "";
Share
10 |10000 characters needed characters left characters exceeded