如何让我的Ember应用程序支持基于用户的多个api主机?

时间:2018-04-04 00:47:48

标签: ember-data ember-cli

在我的Ember应用程序中,我希望api的url基于登录的用户。例如,用户1可能需要使用host1.example.com,而用户2可能需要使用host2.example。融为一体

我可以根据功能在适配器上设置主机吗?例如:

export default DS.JSONAPIAdapter.extend({
  host: () => {
    if (user1) { return 'host1.example.com'; }
    else { return 'host2.example.com'; }
  }
});

1 个答案:

答案 0 :(得分:2)

我建议您使用计算属性和用户服务,而不是使用函数并尝试在您的适配器上手动设置(命令性),因为您随后会声明事情应该如何作为属性更改。这样的事情应该可以很好地运作:

export default DS.JSONAPIAdapter.extend({
  user: service(),
  host: computed(‘user.isWhitelabeled’, function() {
    if (this.get(‘user.isWhitelabeled’)) {
      return 'host1.example.com';
    } else {
      return 'host2.example.com';
    }
  })
});