1

我是一名 Ember 新手(使用 ember-cli)并开始在应用程序上工作,我有一个 facebook 登录,该登录会触发一个登录对话框,该对话框成功地将我登录到 facebook,但在控制台中出现“未捕获的 ReferenceError:checkLoginState 未定义”。

我正在使用 facebook sdk 代码的初始化程序:app/initializers/facebook-sdk.js

/* global FB */

export function initialize(application){
  application.deferReadiness();
};
export default {
  name: 'facebook-sdk',

  initialize: function(){
    var fbAsyncInit = function() {
      FB.init({
        appId: 'XXX',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true,
        version: 'v2.7'
    });
  };

(function(d, s, id){
  var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = fbAsyncInit;
}

和我的登录按钮:app/templates/components/login-form.hbs

<button {{action 'authenticateWithFacebook'}} class="btn btn-primary" data-auto-logout-link="true" style="cursor: pointer;" > Login</button>

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();" data-auto-logout-link="true"> Connect with Facebook

我的组件文件夹中的所有操作代码:app/components/login-form.js

/* global FB */
import Ember from 'ember';
const{ inject: {service}, Component} = Ember;

export default Component.extend({
  session: service('session'),
  torii: service(),

  actions: {

    authenticateWithFacebook() {
      FB.login(function(response){
        console.log(response);
      });
    },

    checkLoginState() {
      FB.login(function(response){
        console.log(response);
      });
    },
    authenticates() {
      let { identification, password } =    this.getProperties('identification', 'password');
      return this.get('session').authenticate('authenticator:devise', identification, password).catch((reason) => {
      this.set('errorMessage', reason.error || reason);
      alert("Hey! I tried, but I don't know how to authenticate.");
      });
    }
  }
});

触发 authenticateWithFacebook 操作的第一个按钮运行良好(甚至在控制台中显示其状态响应 == 已连接以及它包含的所有内容,例如 accessToken、expiresIn.. 等)。只有当我单击第二个按钮时才会出现错误。所以我假设 SDK 需要在调用动作之前先加载。如果是这样的话,我不确定是否应该将 SDK 代码和 FB 登录方法分开,或者将它们都放在同一个文件夹中。

任何帮助或澄清将不胜感激。或者让我知道是否需要添加更多代码以使其更加清晰。谢谢你。

4

0 回答 0