注文オブジェクトの価格を計算するために使用されている一連の為替レートがあり、必要な場所でこれらを利用できるようにする最善の方法を見つけようとしています (モデル、基本的にフラットな Ember.Object、およびコントローラー) )。
私は良い解決策をたくさん探しました.依存性注入についての多くの話と同様に、機能していないように見える古いコード例がたくさんありますが、それを使用する方法の良い例はありません.
だから私はこれに頼りました。これはちょっと汚れているように感じますが、この方法で必要な場所で window.App.exchangeRates を呼び出すことができます:
var ApplicationController = Ember.Controller.extend({
init: function() {
this._super();
var exchangeRates = new ExchangeRates();
exchangeRates.refresh();
this.set('exchangeRates', exchangeRates);
// Expose the exchangeRates to the global scope.
window.App.exchangeRates = exchangeRates;
},
});
より具体的には、次のようにルーターによって作成されたモデルに注入する必要があります。
var BuyBankTransferIndexRoute = Ember.Route.extend({
setupController: function (controller) {
controller.set('model', BuyOrder.create({
type: 'bank_transfer',
}));
}
});
次に、次のようにモデル内にバインドします。
var BuyOrder = Ember.Object.extend({
init: function () {
// Whenever the currency data reloads.
window.App.exchangeRates.addObserver('dataLoaded', function () {
// Trigger a re-calculation of the btcPrice field.
this.propertyWillChange('currency');
this.propertyDidChange('currency');
}.bind(this));
},
});
より良い方法はありますか?ここでは Ember 1.2.0 で Ember App Kit を使用しています。