我有一个Ember适配器,它连接到我的api以将数据提供给模型和商店。问题是由于"原因"我的api的地址可能会改变。如何更新我的适配器,以便商店中的更改将更新主机?即使我更改主机,适配器也会在下次需要发出HTTP请求时使用新主机吗?
这是我尝试过的......
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;
我有一个观察者正在观察商店何时发生变化,然后调用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);
})
});
答案 0 :(得分:0)
我能够弄清楚如何解决我的问题。我可以使用buildURL函数轻松更新适配器的URL,并使用实用程序从商店中获取所需的ip。我不需要观察者或任何静态属性。它最终看起来像这样......
const ApplicationAdapter = RestAdapter.extend({
namespace: 'api',
buildURL(): function {
return utility.ip() + this.get('namespace');
}
});