余烬数据模型中的json子文档 [英] json subdocuments in ember data model

查看:72
本文介绍了余烬数据模型中的json子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,返回的内容是这样的,其中有一个固定的信封,然后是一个json主体,其样式取决于 body_schema。我希望能够使用ember-data来管理这些信息,其中头等字段用于固定信封,而对象仅用于body字段。这可能吗?我似乎在文档中看不到任何类似内容,但我无法想象我是第一个遇到此问题的人。

I have a web service that returns something like this, where there is a fixed envelope and then a body that is json with a schema that depends on "body_schema". I'd like to be able to use ember-data to manage these, with first class fields for the fixed envelope and just an object for the body field. Is this possible? I can't seem to see anything like this in the docs but I can't imagine I'm the first with this issue.

{"messages":
[
  {"id":"5",
   "from": "someone",
   "to": "somebody",
   "body_schema": "atype", 
   "body": {
     {"url":"http://localhost:3030/blobs/511d63ddd0a6b5e863000001"}
   }
  }
]}

有什么想法吗?

推荐答案

我解决了这个问题-您需要做的是将自己的属性转换添加到现有的JSONTransforms集。我在app.js中定义了这样的对象转换:

I solved this - what you need to do is add your own attribute transform to the existing set of JSONTransforms. I defined an object transform like this in my app.js:

DS.JSONTransforms.object = {
    deserialize: function(serialized) {
      return Em.isNone(serialized) ? {} : serialized;
    },
    serialize: function(deserialized) {
      return Em.isNone(deserialized) ? {} : deserialized;
    }
};

有了这个,我可以定义这样的消息模型:

With this in place, I can define a messages model like this:

App.Message = DS.Model.extend({
  timestamp: DS.attr('date'),
  body: DS.attr('object'),
  ...
});

这篇关于余烬数据模型中的json子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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