简体   繁体   English

将检索到的Ember Data记录转换为普通对象的Ember方法是什么?

[英]What is the Ember way of converting retrieved Ember Data records into plain objects?

I have retrieved a series of records using var items = store.find('model'); 我使用var items = store.find('model');检索了一系列记录var items = store.find('model'); . The returned object is an instance of RecordArray , and contains several entries, each with an Ember object that allows me to get and set properties into the records. 返回的对象是RecordArray一个实例, RecordArray包含几个条目,每个条目都有一个Ember对象,允许我在记录中获取和设置属性。

It all looks pretty good. 一切看起来都不错。

Now I need to feed the returned objects into a third party library, and of course I cannot send Ember objects there since it expects plain objects. 现在我需要将返回的对象提供给第三方库,当然我不能发送Ember对象,因为它需要普通对象。

I looked on pages and pages of related material but I can't find any generic way of doing this. 我查看了相关材料的页面和页面,但我找不到任何通用的方法。 I'm pretty sure there is one since this seems to be a very basic use case, so I don't want to reinvent the wheel and write it all again. 我很确定有一个,因为这似乎是一个非常基本的用例,所以我不想重新发明轮子并再次写出来。

Is there a facility in Ember for that? Ember那里有设施吗? How can I obtain a simple array with plain JavaScript objects (just hashes, I mean) from this RecordArray I got? 我如何从这个RecordArray获得一个简单的数组,其中包含普通的JavaScript对象(我的意思是哈希)?

UPDATE UPDATE

Of course I can do JSON.parse(JSON.stringify(recordArray)); 当然我可以做JSON.parse(JSON.stringify(recordArray)); but for large objects that doesn't seem too performant with so many conversions. 但是对于那些看起来效果不太好的大型对象而言。 I'm wondering if Ember provides a more direct way (with better performance) of doing this. 我想知道Ember是否提供了更直接的方式(具有更好的性能)。

Thanks! 谢谢!

As far as I know there is no ObjectSerializer so probably easiest way is to use JSONSerializer and use JSON.parse to create objects out of them. 据我所知,没有ObjectSerializer,所以最简单的方法是使用JSONSerializer并使用JSON.parse创建对象。

items.map(function(e){
  return JSON.parse(e.toJSON());
});

However, you can manually write serialization logic. 但是,您可以手动编写序列化逻辑。

function serializeToObject(model){
  var fields = Ember.get(model.constructor, 'fields');
  obj = {};
  fields.forEach(function(fieldName, kindOfField){
    obj[fieldName] = model.get(fieldName);
  });
  return obj;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM