Ember 模型到 json [英] Ember model to json
问题描述
我正在寻找一种有效的方法将我的 Ember 对象转换为 json 字符串,以便在下面的 websocket 消息中使用它
I am looking for an efficient way to translate my Ember object to a json string, to use it in a websocket message below
/*
* Model
*/
App.node = Ember.Object.extend({
name: 'theName',
type: 'theType',
value: 'theValue',
})
websocket 方法:
The websocket method:
App.io.emit('node', {node: hash});
hash 应该是节点的 json 表示.{名称:名称,类型:类型,..}必须有一个快速的在线人员才能做到这一点..我不想手动进行,因为我有很多属性而且它们很可能会改变..
hash should be the json representation of the node. {name: thename, type: theType, ..} There must be a fast onliner to do this.. I dont want to do it manualy since i have many attributes and they are likely to change..
推荐答案
Ember Data Model
的对象使用 toJSON
方法计数,该方法可选择接收带有 includeId
属性用于将 Ember 数据模型转换为具有模型属性的 JSON.
Ember Data Model
's object counts with a toJSON
method which optionally receives an plain object with includeId
property used to convert an Ember Data Model into a JSON with the properties of the model.
https://api.emberjs.com/ember-data/2.10/classes/DS.Model/methods/toJSON?anchor=toJSON
您可以按如下方式使用它:
You can use it as follows:
const objects = models.map((model) => model.toJSON({ includeId: true }));
希望有帮助.享受!
这篇关于Ember 模型到 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!