Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌OAuth2+;主干+;Require.js=存在问题;这";结合_Javascript_Backbone.js_Google Api_Oauth 2.0_Requirejs - Fatal编程技术网

Javascript 谷歌OAuth2+;主干+;Require.js=存在问题;这";结合

Javascript 谷歌OAuth2+;主干+;Require.js=存在问题;这";结合,javascript,backbone.js,google-api,oauth-2.0,requirejs,Javascript,Backbone.js,Google Api,Oauth 2.0,Requirejs,GoogleOAuth2代码是基于 LoginView有几个函数,每个函数调用另一个函数 当它到达checkAuth函数时,this.handleAuthResult返回未定义,因为checkAuth是在setTimeout内由handleClientLoad调用的 如何处理这个上下文问题?我可以对变量执行相同的操作吗?scope,clientId,apiKey,而不是将它们设置为全局变量 define(['underscore','jquery','backbone','text!templ

GoogleOAuth2代码是基于

LoginView
有几个函数,每个函数调用另一个函数

当它到达
checkAuth
函数时,
this.handleAuthResult
返回未定义,因为
checkAuth
是在setTimeout内由
handleClientLoad
调用的

如何处理
这个
上下文问题?我可以对变量执行相同的操作吗?
scope
clientId
apiKey
,而不是将它们设置为全局变量

define(['underscore','jquery','backbone','text!templates/login.html','async!https://apis.google.com/js/client.js!onload'], function(_, $, Backbone, loginTpl) {
  var LoginView = Backbone.View.extend({
    template: _.template(loginTpl),

    initialize: function() {
      clientId = '';
      apiKey = '';
      scopes = 'https://www.googleapis.com/auth/plus.me';

      this.handleClientLoad();
    },

    render: function() {
      this.$el.html(this.template());
      return this;
    },

    handleClientLoad: function() {
      gapi.client.setApiKey(apiKey);
      window.setTimeout(this.checkAuth, 1);
    },

    checkAuth: function() {
      gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true }, this.handleAuthResult);
    },

    handleAuthResult: function(authResult) {
      var authorizeButton = this.$el.find('#authorize-button');

      if (authResult && !authResult.error) {
        console.log('Authorized!');
        this.makeApiCall();
      } else {
        authorizeButton.onclick = this.handleAuthClick;
      }
    },

    handleAuthClick: function(event) {
      gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, this.handleAuthResult);
      return false;
    },

    makeApiCall: function() {
      gapi.client.load('plus', 'v1', function() {
        var request = gapi.client.plus.people.get({
          'userId': 'me'
        });
        request.execute(function(resp) {
          var authorizeButton = this.$el.find('#authorize-button');
          localStorage.isAuthenticated = true;
          Backbone.history.navigate('', true);
        });
      });
    }
  });

  return LoginView;
});

handleClientLoad中的setTimeout是一个问题:

window.setTimeout(this.checkAuth, 1);
当'window.setTimeout'在1ms后执行,并且不再在LoginView范围内执行时

您可以使用将执行绑定到此

window.setTimeout(_.bind(this.checkAuth, this), 1);
你也可以阅读帖子


希望这有帮助

handleClientLoad中的setTimeout是一个问题:

window.setTimeout(this.checkAuth, 1);
当'window.setTimeout'在1ms后执行,并且不再在LoginView范围内执行时

您可以使用将执行绑定到此

window.setTimeout(_.bind(this.checkAuth, this), 1);
你也可以阅读帖子


希望这有帮助

handleClientLoad中的setTimeout是一个问题:

window.setTimeout(this.checkAuth, 1);
当'window.setTimeout'在1ms后执行,并且不再在LoginView范围内执行时

您可以使用将执行绑定到此

window.setTimeout(_.bind(this.checkAuth, this), 1);
你也可以阅读帖子


希望这有帮助

handleClientLoad中的setTimeout是一个问题:

window.setTimeout(this.checkAuth, 1);
当'window.setTimeout'在1ms后执行,并且不再在LoginView范围内执行时

您可以使用将执行绑定到此

window.setTimeout(_.bind(this.checkAuth, this), 1);
你也可以阅读帖子


希望这有帮助

感谢这部分帮助,但是我如何在
var authorizeButton=this.$el.find(“#authorizeButton”)中处理
this.$el
?更新:使用
.bindAll解决(这是'handleAuthResult','makeApiCall')
。感谢这部分帮助,但是我如何处理
这个。
var authorizeButton=this.$el.find(“#authorizeButton”)?更新:使用
.bindAll解决(这是'handleAuthResult','makeApiCall')
。感谢这部分帮助,但是我如何处理
这个。
var authorizeButton=this.$el.find(“#authorizeButton”)?更新:使用
.bindAll解决(这是'handleAuthResult','makeApiCall')
。感谢这部分帮助,但是我如何处理
这个。
var authorizeButton=this.$el.find(“#authorizeButton”)?更新:使用
.bindAll解决(这是'handleAuthResult','makeApiCall')。此问题可能与您的问题不同。您是如何使用此库解决身份验证和安全问题的?它是否返回令牌?此问题可能与您的问题不同。您是如何使用此库解决身份验证和安全问题的?它是否返回令牌?此问题可能与您的问题不同。您是如何使用此库解决身份验证和安全问题的?它是否返回令牌?此问题可能与您的问题不同。您是如何使用此库解决身份验证和安全问题的?它是否返回令牌?