2

Following is my code :

        var me = this;
        gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, function (authResult: any) {
            if (authResult && !authResult.error) {
                me.accessToken = authResult.access_token;

            } else {

              //TODO : show error in front end
            }
        });

when i use callback function like this.

gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, AuthResult);

function AuthResult(authResult: any) {
                if (authResult && !authResult.error) {
                    me.accessToken = authResult.access_token;

                } else {

                  //TODO : show error in front end
                }

I dont get the me property in that callback function

How i can wrap the callback function in other callback function where i can get the scope also in JS

2 Answers 2

1

Use a fat arrow:

    gapi.auth.authorize({ client_id: client, scope: scope, immediate: true },AuthResult);

    const AuthResult = (authResult: any) => {
            if (authResult && !authResult.error) {
                this.accessToken = authResult.access_token;

            } else {
              //TODO : show error in front end
            }

More

Don't use .bind : https://basarat.gitbooks.io/typescript/content/docs/tips/bind.html at least not yet

Sign up to request clarification or add additional context in comments.

1 Comment

if i have to use the same work in javascript instead of typescript. Bind would be fine then or harmful as described in link too/
0

Use .bind:

gapi.auth.authorize(..., AuthResult.bind(this));

function AuthResult(...) {
  // use `this` instead of `me`
}

See also How to access the correct `this` context inside a callback?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.