迭代Ember.js ember-data记录数组 [英] Iterating over Ember.js ember-data Record Arrays

查看:100
本文介绍了迭代Ember.js ember-data记录数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都一直在这个问题上反对这个问题,我觉得我很接近一个解决方案,但是不能完全实现。我正在使用带有Ember-Data和Fixtures适配器的Ember.js,最终迁移到REST适配器。基本的问题是这样的:我有网站和主管,有一个多对多关系。我想向用户介绍现有网站/主管配对的选择框,按网站排序,即:




  • 网站1 - 主管1

  • 网站1 - 主管2

  • 网站2 - 主管1 (记住,多对多)

  • 网站2 - 主管3



我需要将这两个资源摆放到一个可以传递到从Ember.Select继承(或将继承)的视图。目前,我正在使用Supervisor控制器上的一个方法来尝试这个方法,该方法叫做flat,因为它会返回一个表示这些关系的扁平数组。控制器如下所示。我在使用.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); //< - 记录一个包含四个记录的对象,
//,属性length显示4
//好!(见下面的日志输出)

console.log(c [0]); //< - 记录第一条记录!!
console.log(c [1]); //< - undefined(?!)
console.log c [2]); //< - undefined(?!)
console.log(c [3]); //< - undefined(?!)
console.log c.get('length')); //< - 1(不是四个?!)

return c; //< - 最终这将返回新构造的数组
});
}
});

这是从第一个console.log()调用

$ b $的日志输出b

  0:对象
1:对象
2:对象
3:对象
__ember1376005434256:ember325
__ember1376005434256_meta:Meta
_super:undefined
length:4
__proto__:Array [0]

你能告诉我我在这里失踪吗?我不知道如何访问四个产生的主管中的每一个。



谢谢!

解决方案

看起来您正在加载模型之前访问模型(您可以在属性中看到isUpdating)。如果你觉得懒洋洋地看着这个,可以稍后再来看看这些项目。或者,您可以在控制器上设置模型并进行渲染,并在模型完成加载时让余烬更新视图。

  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



祝你好运Ember!

  someArray:function(){
var arr = Ember.A();
this.get('model')forEach(function(item){
item.get('sites')。forEach(function(site){
arr.pushObject(someObject) ; //一些表示每个特定组合的对象
});
});
} .property('model')


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-many relationship. I want to present the user with a select box for their existing site/supervisor pairings, sorted by site, ie:

  • Site 1 - Supervisor 1
  • Site 1 - Supervisor 2
  • Site 2 - Supervisor 1 (remember, many to many)
  • Site 2 - Supervisor 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 appears to 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:

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

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!

解决方案

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...

 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

Good luck with 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')

这篇关于迭代Ember.js ember-data记录数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆