0

I need to Accessing Global Value in side JavaScript success function( logonSuccessCallback) How can i accessing Global Value. Please suggest me to fix this.Here i am using cordova Plugin Its not supporting ionic native.

import { Component , NgZone } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalProvider } from "../../providers/global/global";
//import { LogonProvider } from "../../providers/logon/logon"; 
//import { Camera, CameraOptions } from '@ionic-native/camera';
//import { Device } from '@ionic-native/device';

//declare var cordova;  
declare var sap: any;
declare var myExtObject: any;
declare var webGlObject: any;
//declare var globalvar;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage { 
todo = {};
sap:any;
globalvar:any;
 slideOneForm: FormGroup;
submitAttempt: boolean = false;
  constructor(public navCtrl: NavController, public formBuilder: FormBuilder,public global: GlobalProvider,private ngZone:NgZone) {//,private camera: Camera,private device: Device
//public logon:LogonProvider,                       
                        this.globalvar=global;
                        // this.slideOneForm = formBuilder.group({   
                                            // firstName: [''],
                                            // lastName: [''],
                                            // age: ['']
                                        // })  
                                        global.appId="com.data.myapp";
                                        this.global.context={
    "serverHost": "134.87.656.193", //Place your SMP 3.0 server name here
    //"serverHost": "hcpms-i82XXXXtrial.hanatrial.ondemand.com", //SAP Cloud Platform Mobile Services
    "https": false,  //true for SAP Cloud Platform Mobile Services
    "serverPort": "8080",  //443 for SAP Cloud Platform Mobile Services
    //"multiUser": true,
    //"useLocalStorage": false,
    "user": "i82XXXX",          //For demo purposes, specify the user name and password you wish to register with here to save typing on the device 
    "password": "XXXXX",        //Note, if you wish to use this user name and password to be passed to the backend OData producer, choose Basic as the SSO mechanism
                                //The AuthProxy plugin with the Logon plugin can respond to 401 Authentication challenges if the same credentials used to register are also used to make OData requests
                                //Once set the credentials can be changed by calling sap.Logon.changePassword()
    "passcode": "password",     //Hardcoding passwords and unlock passcodes are strictly for ease of use during development
                                //Passcode can be changed by calling sap.Logon.managePasscode()
    "unlockPasscode": "password",
    "passcode_CONFIRM": "password",
    "oldPasscode" : "password",
    "communicatorId": "REST",

    //"auth": [ { "type": "saml2.web.post" } ], //Indicates that a redirect to a SAML IDP should be used for registration
    //"refreshSAMLSessionOnResume": "skip",  // Useful for offline apps when you may not wish for a saml check to be performed when the app is resumed since you may be offline

    "custom": {
        "hiddenFields": ["farmId", "resourcePath", "securityConfig", "serverPort", "https"]
    }
};
                                          this.slideOneForm = formBuilder.group({
                                                                        firstName: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])],
                                                                        lastName: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])],
                                                                        age: ['']
                                                                    });
                                                                     // save1(){
                                                                         // alert("Hi");
                                                                     // }
  }
  save(){
      console.log(testvar);

      console.log(this.globalvar);  
    this.submitAttempt = true;
    if(!this.slideOneForm.valid){
        alert("Failure ");
    }
    else{
sap.Logon.init(logonSuccessCallback, logonErrorCallback, this.global.appId, this.global.context);
    }

     function logonSuccessCallback(result) {
        console.log(this);
        console.log(result);
        this.globalvar.applicationContext=result;
        console.log(this.globalvar.applicationContext)
    }
     function logonErrorCallback(error) {   //this method is called if the user cancels the registration.
                console.log("An error occurred:  " + JSON.stringify(error));

            }

  }
  logForm() {
    console.log(this.todo);
  }


}

Global value inside save working fine.When its Calling to success callback then this Key word also printing undefined.

  • This has nothing to do with ionic native _or_ global variable.. `globalvar:any` is a class variable – Suraj Rao Oct 24 '18 at 09:20
  • You need to use arrow functions in callbacks – Suraj Rao Oct 24 '18 at 09:21
  • 1
    Possible duplicate of [When should I use Arrow functions in ECMAScript 6?](https://stackoverflow.com/questions/22939130/when-should-i-use-arrow-functions-in-ecmascript-6) – Suraj Rao Oct 24 '18 at 09:21
  • Arrow Function Means Like This ` Logon.init(function (Sucess, error) { (Sucess) ? console.log('Download error!', Sucess) : console.error('Download finished', error); }, this.global.appId, this.global.context);` – J.Gowri Sankar Reddy Oct 24 '18 at 10:18
  • How we will write Arrow Functions `sap.Logon.init(logonSuccessCallback, logonErrorCallback, this.global.appId, this.global.context); var logonSuccessCallback=(result)=>{console.log(result)} ` – J.Gowri Sankar Reddy Oct 24 '18 at 10:22
  • `sap.Logon.init((result)=>logonSuccessCallback(result),(err)=> logonErrorCallback(err), this.global.appId, this.global.context);` or use bind `sap.Logon.init(logonSuccessCallback.bind(this), logonErrorCallback.bind(this), this.global.appId, this.global.context);` – Suraj Rao Oct 24 '18 at 10:27
  • Nothing To change in this function ** ` function logonSuccessCallback(result) { console.log(this); console.log(result); this.globalvar.applicationContext=result; console.log(this.globalvar.applicationContext) } function logonErrorCallback(error) { //this method is called if the user cancels the registration. console.log("An error occurred: " + JSON.stringify(error)); }`** – J.Gowri Sankar Reddy Oct 24 '18 at 10:31
  • not necessary...you need to ensure it is called with the right calling context. Also it needs to be outside the `save()` function.. Why define inside save? – Suraj Rao Oct 24 '18 at 10:32
  • Thanks Suraj Rao. Its working fine – J.Gowri Sankar Reddy Oct 24 '18 at 10:43
  • .bind(this) Working fine.`sap.Logon.init((result) =>logonSuccessCallback(result), (err) => logonErrorCallback(err), this.global.appId, this.global.context);`getting SCRIPT1002: Syntax error – J.Gowri Sankar Reddy Oct 24 '18 at 10:53

0 Answers0