在最新的Ember Data版本中使用RESTSerializer格式化JSON [英] Formatting JSON using the RESTSerializer in the latest Ember Data version

查看:107
本文介绍了在最新的Ember Data版本中使用RESTSerializer格式化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将我的JSON压缩"为正确的格式.

I'm struggling to 'munge' my JSON into the correct format.

为了说明我做得很快,JSfiddle.

To illustrate i've made a quick, JSfiddle.

http://jsfiddle.net/chrismasters/NQKvy/638/

服务器返回数据的格式与Ember Data现在推荐的首选格式有一些区别.

The format the server returns the data has a couple of differences to the preferred format recommended by Ember Data now.

这是原始JSON输出

{
    "video": {
        "uuid": "8a660002-03c6-4b8e-bd8b-4ce28fa0dacd",
        "state": "pending",
        "theme": "basic",
        "resolution": "nHD",
        "title": "Test title",
        "track": {
            "uuid": "376fc3bb-d703-49e7-9d92-bce7f6bf8b56",
            "state": "complete",
            "source": "upload"
        }
    }
}

第一个是,它使用字符串形式的UUID而不是使用ID.

The first is that rather than use IDs it uses a UUID that is a string.

我似乎至少已经对视频使用了normalizeHash进行了修复-但我不确定是否也可以使用相同的方法来修复轨道模型-尤其是在需要时使用嵌入的情况下.

I seem to have managed to fix that using the normalizeHash, for video at least - but i'm not sure whether the same approach will fix the track model too - especially if I use embedding as I need to.

这是开始出现大问题的地方,如果我从视频模型中注释掉EmiratesTo关系,那么它工作正常,所以我认为...显然,这是嵌入式轨道数据的JSON格式问题.

This is where the big problems start to appear, if I comment out the belongsTo relationship from the video model then it works OK, so I think... it is clearly a problem with the JSON formatting for the embedded track data.

这是模型定义和序列化

App.Video = DS.Model.extend({
  title: DS.attr('string'),
  //track: DS.belongsTo('track', { embedded: true })
});

App.VideoSerializer = DS.RESTSerializer.extend({
    normalizeHash: {
        video: function(hash) {
            hash.id = hash.uuid;
            delete hash.uuid;
            return hash;
        }
    }
});

我非常感谢有关如何将该响应格式化为Ember Data可以识别的格式的一些建议.

I'd really appreciate some advice on how to format this response into a format that Ember Data recognises.

而且-有人知道调试这些序列化转换的工具或好的方法,因为目前Ember的错误消息在调试或查看序列化输出是什么方面不是很有帮助.

Also - does anyone know of a tool or good way of debugging these serialization transformations because at the moment the error message from Ember is not very helpful in terms of debugging or seeing what the serialization output is.

非常感谢您提出的任何帮助.

Many thanks for any help you can suggest.

克里斯

推荐答案

万一其他人对序列化有相同的困惑,我想我会附上解释如何解决此问题的说明.

In case anyone else has the same confusion over serializations I thought i'd include an explanation how to solve this problem.

这是正在工作的jsbin:

Here is the working jsbin:

http://jsbin.com/fuzu/4

要点是:

主键

primaryKey: 'uuid'

对于将ID转换为正确的命名& ;;需要明确地应用于任何序列化程序(在ApplicationSerializer上全局使用似乎无效).

Is useful to convert the id into the correct naming & needs to be applied explicitly to any serializers (using globally on a ApplicationSerializer didn't seem to work).

模型关系

track: DS.belongsTo('track', {embedded: true} )

确保关系的定义包括嵌入&仅在一侧.

Ensure the definition of the relationship includes embedding & only on one side.

提取单个

extractSingle: function(store, type, payload, id, requestType) {
    var tracks = [];
    var track = payload.video.track;
    var video = payload.video;

    tracks.push(track);

    video.track = payload.video.track.uuid;

    payload = { video: video, track: tracks };

    return this._super(store, type, payload, id, requestType);
}

对于模型来说,多元关系对于Ember Data理解关系确实非常重要.即使模型关系是一个belongsTo.

Pluralization is really important for Ember Data to understand the relationships, even though the model relationship is a belongsTo.

您可以在所需的(有效的)JSON中清楚地看到这一点

You can see this clearly in the desired (working) JSON

{
    "video": {
        "id": "8a660002-03c6-4b8e-bd8b-4ce28fa0dacd",
        "state": "pending",
        "theme": "basic",
        "resolution": "nHD",
        "title": "Test title",
        "track": "2"
    },
  "track": [{
         "id": "2",
         "state": "complete",
         "source": "upload"
     }]
}

视频中的跟踪值不是包裹在数组中,而根跟踪值是数组.

The track value in video isn't wrapped in an array, yet the root track value is an array.

由于这个原因,我发现首先定义所需的JSON并对其进行测试,然后再尝试将真正的JSON修改为该格式非常有用.

For this reason I found it very useful first define the desired JSON and test it working first, then try to munge the real JSON into that format.

我认为,用于帮助此过程(可视化来自序列化的实时JSON输出)的工具可能对Ember Data&我将要研究的东西.

I think a tool to help with this process (visualising real-time JSON output from seraliziation) could be a great addition to Ember Data & something I'm going to look into creating.

这篇关于在最新的Ember Data版本中使用RESTSerializer格式化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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