Caleb Sayre
Caleb Sayre

Reputation: 401

How can I have my Ember app support multiple api hosts, based on the user?

In my Ember app, I want the url to the api be based on the user that is logged in. For example, user 1 may need to use host1.example.com and user 2 may need to use host2.example.com.

Can I have set the host on the adapter based on a function? For example something like this:

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

Upvotes: 0

Views: 38

Answers (1)

acorncom
acorncom

Reputation: 5955

Instead of using a function and trying to set something manually (imperatively) on your adapter, I’d suggest using a computed property and your user service, as you are then declaring how things should act as properties change. Something like this should work pretty well:

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

Upvotes: 2

Related Questions