javascript 迭代 Ember.js ember-data 记录数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18138383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating over Ember.js ember-data Record Arrays
提问by Ben
I've been beating my head against this problem all day, and I feel like I'm close to a solution but just can't quite make it happen. I'm using Ember.js with Ember-Data and the Fixtures adapter, eventually migrating to a REST adapter. The basic problem is this: I have Sites and Supervisors, with a many-to-manyrelationship. I want to present the user with a select box for their existing site/supervisor pairings, sorted by site, ie:
我整天都在努力解决这个问题,我觉得我已经接近解决方案了,但就是无法实现。我将 Ember.js 与 Ember-Data 和 Fixtures 适配器一起使用,最终迁移到 REST 适配器。基本问题是:我有站点和主管,它们是多对多的关系。我想为用户提供一个选择框,用于选择他们现有的站点/主管配对,按站点排序,即:
- Site 1 - Supervisor 1
- Site 1 - Supervisor 2
- Site 2 - Supervisor 1 (remember, many to many)
- Site 2 - Supervisor 3
- 站点 1 - 主管 1
- 站点 1 - 主管 2
- 站点 2 - 主管 1 (记住,多对多)
- 站点 2 - 主管 3
I need to wrangle these two resources into a single array that I can pass to a view which inherits (or will inherit) from Ember.Select. Currently I'm attempting this with a method on the Supervisors controller which I'm calling "flat", because it will return a flattened array representing these relationships. The controller is shown below. I'm using .find().then() to process the data after the promise has been fulfilled. The data that I get back appearsto contain all four of my fixtures, but when I try any of the enumerable methods on them (notably forEach), it behaves as if it has only returned the first object. I have tried iterating over the data object as well as data.get('content'). I'm quite new to Ember, so maybe I'm going about this the wrong anyway, but regardless this seems very strange to me. Here's my code:
我需要将这两个资源整合到一个数组中,我可以将其传递给从 Ember.Select 继承(或将继承)的视图。目前,我正在使用我称之为“扁平”的 Supervisor 控制器上的一个方法来尝试这个,因为它将返回一个表示这些关系的扁平数组。控制器如下图所示。我正在使用 .find().then() 来处理承诺完成后的数据。我取回的数据出现包含我的所有四个装置,但是当我在它们上尝试任何可枚举方法(特别是 forEach)时,它的行为就好像它只返回了第一个对象。我试过迭代数据对象以及 data.get('content')。我对 Ember 还很陌生,所以无论如何我都可能在这方面做错了,但无论如何这对我来说似乎很奇怪。这是我的代码:
App.SupervisorsController = Ember.ArrayController.extend({
flat: function(){
return App.Supervisor.find().then(function(data){
var c = data.get('content') ;
console.log(c) ; // <-- logs an object containing four records,
// with attribute "length" showing 4
// Great! (see below for log output)
console.log(c[0]) ; // <-- logs first record. Great!
console.log(c[1]) ; // <-- undefined (?!)
console.log(c[2]) ; // <-- undefined (?!)
console.log(c[3]) ; // <-- undefined (?!)
console.log(c.get('length')) ; // <-- 1 (not four?!)
return c ; // <-- eventually this will return the newly constructed array
}) ;
}
}) ;
And here's the log output from the first console.log() call
这是第一个 console.log() 调用的日志输出
0: Object
1: Object
2: Object
3: Object
__ember1376005434256: "ember325"
__ember1376005434256_meta: Meta
_super: undefined
length: 4
__proto__: Array[0]
Can you tell me what I'm missing here? I can't figure out how to access each of the four resulting supervisors.
你能告诉我我在这里缺少什么吗?我无法弄清楚如何访问四个由此产生的主管中的每一个。
Thanks!
谢谢!
回答by Kingpin2k
It appears like you're accessing the models before they are finished loading (You can see this in the property, isUpdating). If you feel like lazily looking at this you can use ember run later to just see the items a wee bit later. Or you can set the model on the controller and render it and let ember update the view when the models are finished loading...
看起来您在完成加载之前访问模型(您可以在属性 isUpdating 中看到这一点)。如果您想懒洋洋地看这个,您可以稍后使用 ember run 来稍后查看项目。或者您可以在控制器上设置模型并渲染它,并在模型加载完成后让 ember 更新视图......
Ember.run.later(function(){
data.forEach(function(item){
console.log(item);
});
}, 2000);
App.ApplicationRoute = Ember.Route.extend({
activate: function(){
this.controllerFor('supervisors').set("model", App.Supervisor.find());
}
});
http://jsbin.com/ijiqeq/12/edit
http://jsbin.com/ijiqeq/12/edit
Good luck with Ember!
祝 Ember 好运!
someArray: function(){
var arr = Ember.A();
this.get('model').forEach(function(item){
item.get('sites').forEach(function(site){
arr.pushObject(someObject); //some object that is represents each specific combination
});
});
}.property('model')