Ember.js 内容准备就绪时通过DS.FilteredRecordArray循环
我的路线定义如下:Ember.js 内容准备就绪时通过DS.FilteredRecordArray循环,ember.js,ember-data,Ember.js,Ember Data,我的路线定义如下: setupController: function (controller, model) { var analyticsRuns, store = this.get('store'), exerciseRunId = this.controllerFor('analyticsContext').get('exerciseRunId'); // Query the server for all runs that belong
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);
}
setAnalyticsRunSelectOptions:函数(){
var选项=[],运行;
//console.log(this.get('content');
//log(this.get('content.length');
//对于(var i=0,len=this.get('content.length');i您没有在setupController
钩子中设置控制器的内容。在setupController
钩子中包括以下行:
controller.set('content', model);
您可以知道何时使用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));
}
谢谢Marcio,这对我很有效。我现在觉得自己超级愚蠢,因为我以前一直在尝试对“analyticsRuns”变量执行“then”!