ember.js - 当内容准备就绪时通过 DS.FilteredRecordArray 循环

  显示原文与译文双语对照的内容
107 2

我的路由定义如下:


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);
 }

但是当我在firebug中记录这个函数的"内容"时,它显示

{ content= [0],store=,manager=,更多。}

但是当我点击( 在firebug中) 时,内容就可以看到了。 我想它是因为setAnalyticsRunSelectOptions被调用时内容还没有被填充。 只有当内容可用时,是否可以触发我的操作?

谢谢,Dee

时间: 原作者:

150 4

你可以使用 then 方法知道什么时候加载了 store.find('analyticsRun') 。 这样你就可以在装载的数据中循环。

更新后的代码如下所示:


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)); 
}

原作者:
57 0

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


controller.set('content', model);

原作者:
...