简体   繁体   English

如何序列化Ember-Data

[英]How to serialize Ember-Data

I need to serialize Ember-data to JSON and use the JSON to feed this JS Library: http://www.jstree.com/docs/json/ 我需要将Ember-data序列化为JSON并使用JSON馈送此JS库: http : //www.jstree.com/docs/json/

I don't how to achieve this. 我不知道如何实现这一目标。 DS.RESTSERIALIZER with DS.EmbeddedRecordsMixin should be able to achieve this but I dont know how to call them in my Ember-CLI app. 具有DS.EmbeddedRecordsMixin的DS.RESTSERIALIZER应该能够实现这一目标,但是我不知道如何在我的Ember-CLI应用中调用它们。 They seem to be built to only speak to a backend. 它们似乎仅用于与后端对话。 My backend is Firebase with the Emberfire adapter and I don't want to change it. 我的后端是带有Emberfire适配器的Firebase,我不想更改它。 Is DS.RESTSERIALIZER what I need to you use? 我需要使用DS.RESTSERIALIZER吗? Should I use JSON.stringify() instead? 我应该改用JSON.stringify()吗?

My model is: 我的模型是:

import DS from 'ember-data';

export default DS.Model.extend({
  text: DS.attr('string'),
  order: DS.attr('number', { defaultValue: 0 }),
  cause: DS.belongsTo('cause', { inverse: 'causeUnitLink', async: true}),
  causeUnit: DS.belongsTo('causeUnit', { inverse: 'causeUnitLinks', async: true}),
  parents: DS.hasMany('causeUnitLink', { inverse: 'children', async: true}),
  children: DS.hasMany('causeUnitLink', {inverse: 'parents', async: true}),
});

I have created the below sterilizer but I don't know how to invoke its methods in my controller so I can turn the ember-data into JSON and feed the JSON to my jsTree component. 我创建了以下消毒器,但是我不知道如何在控制器中调用其方法,因此我可以将ember-data转换为JSON并将JSON馈入jsTree组件。

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    text: { embedded: 'always' },
    children: { embedded: 'always' }
  },
});

我发现的最好方法是使用toJSON()方法: http : //emberjs.com/api/data/classes/DS.Model.html#method_toJSON

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

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