如何根据用户支持我的Ember应用程序支持多个API主机



在我的ember应用中,我希望API的URL基于登录的用户。例如,用户1可能需要使用host1.example.com和用户2可能需要使用host2.example.com。

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

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

而不是使用函数并尝试在适配器上手动设置某些东西,我建议使用计算的属性和您的用户服务,因为您然后宣布事物应该如何充当属性的变化。这样的事情应该很好:

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

最新更新