私は何か間違ったことをしていると思いますが、何がわかりません。
私のアプリケーションが読み込まれると、すべての会社を取得する必要があり、それらが到着するとactiveCompany
、 myにプロパティを設定する必要がありますApplicationController
。しかし、オブザーバーをバインドすると、データがロードされる前に発火しますcontent.isLoaded
。CompaniesController
応用
App = Ember.Application.create({
ApplicationController : Ember.Controller.extend({
needs: ['companies'],
activeCompany: null,
activateCompany: function(company) {
this.set('activeCompany',company);
}
})
});
ルーター
App.ApplicationRoute = Ember.Route.extend({
enableLogging : true,
setupController: function(controller, model) {
this.controllerFor('companies').set('content', App.Company.find());
}
});
会社コントローラー
App.CompaniesController = Em.ArrayController.extend({
needs: ['application'],
activateCompany: function() {
console.log(this.get('content.length')); // 0
console.log(this.get('content.isLoaded')); // true
console.log(this.get('content.firstObject')); // undefined
this.get('controllers.application').activateCompany(this.get('content.firstObject'));
}.observes('content.isLoaded')
});
content.isLoaded
データがロードされていないときにが起動するのはなぜですか?
activeCompany
私のコンセプトは間違っているかもしれませんが、私のアプリケーションの残りの部分は、他のデータを取得するために に依存しています。また、プロパティを設定する「company-switcher」もありactiveCompany
ます。
オブザーバーを変更するcontent.@each
と、配列内にあるすべてのアイテムに対して起動します。
編集
次のように回避できます。
App.CompaniesController = Em.ArrayController.extend({
needs: ['application'],
activateCompany: function() {
if (this.get('content.length') > 0)
this.get('controllers.application').activateCompany(this.get('content.firstObject'));
}.observes('content.firstObject.isLoaded')
});
これは、私の変更時にのみ発生しfirstObject
ます。