1

私は何か間違ったことをしていると思いますが、何がわかりません。

私のアプリケーションが読み込まれると、すべての会社を取得する必要があり、それらが到着するとactiveCompany、 myにプロパティを設定する必要がありますApplicationController。しかし、オブザーバーをバインドすると、データがロードされる前に発火しますcontent.isLoadedCompaniesController

応用

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ます。

4

1 に答える 1

0

を使用する必要があることがわかりましたfindQuery。このように前に試してみましApp.Store.findQuery(App.Company)たが、うまくいきませんでした。正しいやり方は次のようになります。

this.controllerFor('companies').set('model', this.get('store').findQuery(App.Company));

経由でストアを取得する必要がありましたthis.get('store')

于 2013-03-20T13:57:39.453 に答える