【问题标题】:ember.js Uncaught TypeError: Object data-size has no method 'transitionTo'ember.js Uncaught TypeError: Object data-size has no method 'transitionTo'
【发布时间】:2013-08-24 23:20:23
【问题描述】:

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

我正在使用ember-facebook.js 库与 facebook 连接。身份验证成功后,我想转换到其他路线,例如'/指数'。这个库在 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”我的索引路由时,我收到以下错误 未捕获的类型错误:对象数据大小没有方法“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');
  }
});

我无法理解它。

非常感谢任何帮助。谢谢

【问题讨论】:

    标签: javascript facebook facebook-graph-api ember.js


    【解决方案1】:

    如何在 Route.beforeModel() 挂钩中访问此对象。

    根据你所说的beforModel钩子的路由,你可以这样做:

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

    根据您的上一条评论进行更新

    您使用的插件稍微过时了,您应用程序中 mixin 的建议实现方法不适用于当前版本的 ember:

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

    从 ember 版本 1.0.0-rc3 开始,您应该这样做:

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

    之后,您应该能够访问上面提到的App.FBUser 对象。

    更新 2

    如果您希望能够在某些事件发生时得到通知,例如 loginlogout 等,您应该(正如插件作者在其博客文章中所述)覆盖 updateFBUser 方法并执行在那里你的过渡。

    由于插件是通过我们的 App 命名空间中可用的 mixin,您应该能够执行以下操作:

    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
      }
    });
    

    希望对你有帮助。

    【讨论】:

    • 我已经更新了解释我想要实现的确切场景的帖子。请指导。
    • 我已经将它更新为使用 App = Ember.Application.creatWithMixins(Ember.Facebook);。我能够与 FB 沟通并更新我的模板。我想要实现的是在收到来自 FB 的成功响应时重定向到其他路由('index')
    • 感谢intuitivepixel。对不起,愚蠢的令人讨厌。我添加了额外的细节。请帮帮我。
    【解决方案2】:

    按照Issue 1添加

    attributeBindings: [],
    

    到:

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

    解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多