如何在用户授权后调用索引/索引路由的模型钩子?

时间:2016-04-07 19:21:39

标签: javascript ember.js firebase ember-data emberfire

您好我正在使用Ember和Firebase创建应用,用户可以在其中创建新帐户,登录并且只有在获得授权后才能查看有关其帐户和某些受保护路由的信息。因此,非授权用户只能看到2条路线:索引(' /'),它显示登录表单和链接到注册路由(' /注册'),显示并允许它们创建一个新账户。我的app / router.js路由器看起来像这样:

Router.map(function() {
  this.route('signup');
  this.route('index', {path: '/'}, function() {
    this.authenticatedRoute('index', {path: '/'});
    this.authenticatedRoute('events');
    this.authenticatedRoute('about');
  });
});

app / templates / index.hbs包含登录表单,当用户登录时,嵌套索引路由被呈现,但是在刷新之前不会调用它的模型钩子。如何使它工作?

应用程序/模板/ index.hbs

{{#unless session.isAuthenticated}}

  {{signin-form store=store session=session authorized="authorized"}}

{{else}}

  {{#link-to 'index.index'  class="item"}}Home{{/link-to}}
  {{#link-to 'index.events' class="item"}}Events{{/link-to}}
  {{#link-to 'index.about'  class="item"}}About{{/link-to}}


  {{outlet}}

{{/unless}}

应用程序/路由/ application.js中

import Ember from 'ember';

export default Ember.Route.extend({
  beforeModel() {
    return this.get("session").fetch().catch(function() {});
  },
  actions: {
    signOut() {
      this.get('session').close();
      this.transitionTo('index');
    },
    accessDenied() {
      this.transitionTo('index');
    },
    authorized() {
      console.log('authorized');
      this.transitionTo('index');
    }
  }
});

应用/组件/登入-form.js

import Ember from 'ember';

export default Ember.Component.extend({
  didRender() {
    this._super(...arguments);
    this.$('form.ui.form').form();
  },

  signIn() {
    let { userPassword, userEmail }
      = this.getProperties('userPassword', 'userEmail');
    console.log('Logging with', userPassword, userEmail);
    this.get("session").open("firebase", { 
        provider: 'password',
        email: userEmail,
        password: userPassword
    }).then((data) => {
      console.log(data);
      this.sendAction('authorized');
    });
  },

  actions: {
    submit() {
      this.signIn();
    }
  }

});

1 个答案:

答案 0 :(得分:0)

在app / routes / application.js中,我已经改变了

authorized() {
  this.transitionTo('index');
}

authorized() {
  this.refresh();
}

现在它有效。但总的来说,我仍然对这个解决方案有疑问,所以如果你看到更好/更好的方式,我会很高兴听到它。