0

我对 ember 很陌生,并试图通过 facebook 实现身份验证

我正在使用ember-facebook.js库与 facebook 连接。身份验证成功后,我想转换到其他路线,例如“/index”。这个库在 mixin 中创建了一个 App.FBUser 对象,该对象是从 facebook 响应中填充的。博客说如下:

每当用户更改(登录、注销、应用程序授权等)时,都会调用 updateFBUser 方法,更新应用程序上的 App.FBUser 对象。你可以用这个绑定做任何你想做的事情,观察它,把它放到 DOM 中,等等。

Ember.Facebook = Ember.Mixin.create({
  FBUser: void 0,
  appId: void 0,
  fetchPicture: true,
  init: function() {
    this._super();
    return window.FBApp = this;
  },
  appIdChanged: (function() {
    var _this = this;
    this.removeObserver('appId');
    window.fbAsyncInit = function() {
      return _this.fbAsyncInit();
    };
    return $(function() {
      var js;
      js = document.createElement('script');
      $(js).attr({
        id: 'facebook-jssdk',
        async: true,
        src: "//connect.facebook.net/en_US/all.js"
      });
      return $('head').append(js);
    });
  }).observes('appId'),
  fbAsyncInit: function() {
    var _this = this;
    FB.init({
      appId: this.get('appId'),
      status: true,
      cookie: true,
      xfbml: true
    });
    this.set('FBloading', true);

    FB.Event.subscribe('auth.authResponseChange', function(response) {
      return _this.updateFBUser(response);
    });

    return FB.getLoginStatus(function(response) {
      return _this.updateFBUser(response);
    });
  },
  updateFBUser: function(response) {
    console.log("Facebook.updateFBUser: Start");
    var _this = this;
    if (response.status === 'connected') {
      //console.log(_this);
      return FB.api('/me', function(user) {
        var FBUser;
        FBUser = user;
        FBUser.accessToken = response.authResponse.accessToken;
        if (_this.get('fetchPicture')) {
          return FB.api('/me/picture', function(path) {
            FBUser.picture = path;
            _this.set('FBUser', FBUser);
            return _this.set('FBloading', false);
          });
        } else {
          _this.set('FBUser', FBUser);
          return _this.set('FBloading', false);
        }
      });
    } else {
      this.set('FBUser', false);
      return this.set('FBloading', false);
    }
  }//updateFBUser
});

更新 :

在我的 LoginController 中添加以下观察者,我能够捕获 App.FBUser 更新事件(从 FB 获得响应后更新;如博客所示)。从这个观察者方法中,当我尝试“transitionTo”我的索引路由时,我收到以下错误 Uncaught TypeError: Object data-size has no method 'transitionTo'。以下是代码

App.LoginController = Ember.Controller.extend({
onSuccess: (function(){
    var self = this;
    /*
        //tried all these method to redirect but error is the same
        var attemptedTransition = this.get('attemptedTransition');
        attemptedTransition.retry();
     */
     /*
    //tried all these method to redirect but error is the same
    var router = this.get('target.router');
    router.transitionTo('index');
    */
    
    //tried all these method to redirect but error is the same
    this.transitionToRoute('index');
}).observes('App.FBUser')
});

索引路线

App.AuthenticatedRoute = Ember.Route.extend({ 
 beforeModel: function(transition){
 var self = this;
 if(!App.FBUser){
  self.redirectToLogin(transition);
 }
},

redirectToLogin: function(transition){
var loginController = this.controllerFor('login');
loginController.set('attemptedTransition', transition);
this.transitionTo('login');
  }
});

我无法理解它。

任何帮助是极大的赞赏。谢谢

4

2 回答 2

2

How can I access this object in my Route.beforeModel() hook.

Depending on what route's beforModel hook you are talking about, this is how you could do it:

App.SomeRoute = Ember.Route.extend({
  beforeModel: function(transition) {
    if (!Ember.isNone(App.FBUser)) {
      // calling 'transitionTo' aborts the transition, redirects to 'index'
      this.transitionTo('index');
    }
  }
});

Update in response to your last comment

The addon you are using is slightly outdated and the proposed implementation method for the mixin in your application will not work with the current version of ember:

App = Ember.Application.create(Ember.Facebook)
App.set('appId', 'yourfacebookappid');

starting from version 1.0.0-rc3 of ember you should rather do it like this:

App = Ember.Application.creatWithMixins(Ember.Facebook);
App.set('appId', 'yourfacebookappid');

After that you should be able to have access to the App.FBUser object as mentioned above.

Update 2

If you want to be able to be notified when some events happend, like login, logout etc. you should (as the Author of the addon states on it's blog post) override the updateFBUser method and do in there your transitions.

Since the addon is trough the mixin available in our App namespace you should be able to do the following:

App = Ember.Application.creatWithMixins(Ember.Facebook, {
  updateFBUser: function() {
    this._super();
    // we are calling super to let the addon
    // do it's work but at the same time we get
    // notified that something happened, so do at this
    // point your transition
  }
});

Hope it helps.

于 2013-08-24T23:36:27.483 回答
0

根据问题 1添加

attributeBindings: [],

至:

return Ember.FacebookView = Ember.View.extend({ 

解决了这个问题。

于 2013-08-29T06:02:26.170 回答