0

I have an Ember Adapter, and it connects to my api to feed data to the models and store. The issue is the address of my api can change because of "reasons". How can I update my adapter so changes in the store will update the host? Even if I change the host will the adapter use the new host next time it needs to make an HTTP Request?

Here is what I have tried...

    const ApplicationAdapter = RestAdapter.extend({
        namespace: 'api',
        host: "0.0.0.0"
    });

    ApplicationAdapter.reopenClass({
        updateHost(host) {
            console.log("Here: " + host);

            //Both of these throw an error probably because host is not static
            //this.set('host', host); I attempted this first
            //this.host = host; Then I attempted this way
        }
    });

export default ApplicationAdapter;

I have an observer that is watching when the store changes, and then calls the static method of ApplicationAdapter.

export default Model.extend({
    ip1: attr('string'),
    ip2: attr('string'),
    ip1Observer: Ember.observer('ip1', function() {
        let newValue = this.get('ip1');
        ApplicationAdapter.updateHost(newValue);
    }),
    ip2Observer: Ember.observer('ip2', function() {
        let newValue = this.get('ip2');
        ApplicationAdapter.updateHost(newValue);
    })
});
1
  • Do remember that you can write model specific adapter if you required. Aug 9, 2016 at 18:26

1 Answer 1

0

I was able to figure out how to solve my issue. I was able to easily update the URL of the Adapter by using the buildURL function, and using a utility to grab from the ip required from the store. I did not need observers, or any static attributes. It ended up looking something like this...

const ApplicationAdapter = RestAdapter.extend({
        namespace: 'api',
        buildURL(): function {
           return utility.ip() + this.get('namespace');
        }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.