0

我的路线定义如下:

setupController: function (controller, model) {
    var analyticsRuns,
        store = this.get('store'),
        exerciseRunId = this.controllerFor('analyticsContext').get('exerciseRunId');

    // Query the server for all runs that belong to this exercise run.
    // The results of the query will be loaded into the store so we can start
    // filtering them, thus taking advantage of live updates.
    // (see http://emberjs.com/guides/models/frequently-asked-questions/)
    store.find('analyticsRun', { 'exerciseRunId': exerciseRunId });

    analyticsRuns = store.filter('analyticsRun', function (analyticsRun) {
        return analyticsRun.get('exerciseRunId') === exerciseRunId;
    });

    controller.set('model', analyticsRuns);
    controller.set('exerciseRunId', exerciseRunId);
    controller.set('analyticsTemplates', store.find('analyticsTemplate'));
    controller.set('exerciseRun', store.find('exerciseRun',exerciseRunId));

    controller.send('setAnalyticsRunSelectOptions');
}

在我的控制器操作中,“setAnalyticsRunSelectOptions”定义为:

    setAnalyticsRunSelectOptions: function () {
        var options = [], run;
        //console.log(this.get('content'));
        //console.log(this.get('content.length'));

        //for(var i= 0,len=this.get('content.length');i<len;i++){
        //    run = this.get('content').objectAt(i);
        //    options.push({"id": run.get('id'), "label": run.get('id') + " " + run.get('name')});
        //}

        console.log(this.get('content'));
        this.get('model').forEach(function (run) {

            options.push({"id": run.get('id'), "label": run.get('id') + " " + run.get('name')});
        });

        this.set('analyticsRunSelectOptions',options);
    }

但是当我在我的萤火虫中记录该功能中的“内容”时,它显示

{ 内容=[0],商店=,经理=,更多...}

但是当我点击它(在萤火虫中)时,可以在那里看到内容。我认为它是因为调用 setAnalyticsRunSelectOptions 时尚未填充内容。有没有办法仅在内容可用时触发我的操作?

谢谢,迪

4

2 回答 2

1

您可以使用该方法知道何时store.find('analyticsRun')加载then。因此,您将能够循环加载加载的数据。

The updated code is the following:

setupController: function (controller, model) {
    var analyticsRuns,
        store = this.get('store'),
        exerciseRunId = this.controllerFor('analyticsContext').get('exerciseRunId');

    // Query the server for all runs that belong to this exercise run.
    // The results of the query will be loaded into the store so we can start
    // filtering them, thus taking advantage of live updates.
    // (see http://emberjs.com/guides/models/frequently-asked-questions/)
    store.find('analyticsRun', { 'exerciseRunId': exerciseRunId }).then(function() {
        controller.send('setAnalyticsRunSelectOptions'); 
    }); 

    analyticsRuns = store.filter('analyticsRun', function (analyticsRun) {
        return analyticsRun.get('exerciseRunId') === exerciseRunId;
    });

    controller.set('model', analyticsRuns);
    controller.set('exerciseRunId', exerciseRunId);
    controller.set('analyticsTemplates', store.find('analyticsTemplate'));
    controller.set('exerciseRun', store.find('exerciseRun',exerciseRunId));   
}
于 2013-10-09T16:40:46.710 回答
0

您没有在setupController钩子中设置控制器的内容。在 中包括以下行setupController

controller.set('content', model);
于 2013-10-09T16:35:39.137 回答