2016-08-09 2 views
0

私はEmberアダプターを持っており、モデルとストアにデータを送るために私のAPIに接続します。問題は私のAPIのアドレスが "理由"のために変わることができることです。どのようにして私のアダプタを更新して、ストアの変更によってホストが更新されるのですか?ホストを変更しても、アダプターは次回にHTTP要求を作成する必要がある新しいホストを使用しますか?ここでEmberアダプターの更新ホスト

は私が店の変化を見ている観測者があり、その後、ApplicationAdapterの静的メソッドを呼び出します...

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; 

を試してきたものです。

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

必要に応じてモデル固有のアダプタを書くことができることを忘れないでください。 – kumkanillam

答えて

0

問題を解決する方法を見つけ出すことができました。私は、buildURL関数を使ってアダプタのURLを簡単に更新することができ、ストアから必要なIPを取得するユーティリティを使用していました。オブザーバーや静的な属性は必要ありませんでした。それはこのような何かを見て終わった...

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