创建一个ember-data应用程序序列化器

时间:2015-06-01 04:52:49

标签: javascript ember.js ember-data json-api

我现在一直试图使用余烬数据。问题是api我不符合strcuture ember-data想要接收的数据。

项:

{
    "data": {
        "id": 1,
        "username": "rodzzlessa",
        "slug": "rodzzlessa"
     }
}

系列:

{
"data": [
    {
        "id": 34,
        "name": "HDG common nails 50lbs",
        "slug": "hdg-common-nails-50lbs4569",
        "description": "Accusantium ipsam impedit omnis sint dolorum.",
        "image_src": "nail-box-angle.png",
        "categories": {
            "data": [
                {
                     "id": 2,
                     "name": "nails",
                    "image_src": "nails-icon.png"
                }
             ]
         }
     }
  ]
}

从我所见,我需要修改extractSingle和extractArray方法。实际上没有一个在线资源显示方法实际上在做什么以及如何修改它们。我检查了ember-data api指南,但是这个例子是如此具体,它不是一个应用程序序列化器,因此它们解决问题似乎更容易。我试过找一本余烬数据书,但它们还没有存在。有没有在线资源可以帮到我?

/ ***** UPDATE SO FAR ****** /

所以我能够创建一个应用程序序列化程序来处理集合和项目:

export default DS.RESTSerializer.extend({

    extractArray: function(store, typeClass, payload) {
        var root_key = Ember.Inflector.inflector.pluralize(typeClass.typeKey);

        payload[root_key] = payload.data;
        delete payload.data;

        return this._super(store, typeClass, payload);
    },

    extractSingle: function(store, typeClass, payload, id){
        var root_key = typeClass.typeKey;

        payload[root_key] = payload.data;
        delete payload.data;

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

现在我唯一的问题是处理关系我开始乱用normalize方法这是我到目前为止所做的:

normalize: function(typeClass, hash, prop){
        self = this;

        typeClass.eachRelationship(function(key, relationship){
            if(relationship.kind === "hasMany"){
                if( ! hash[key]){
                    return hash;
                }

                var hashed = hash[key].data;

                hash[key] = hashed;

                return hashed;
            }
        });

        return this._super(typeClass, hash, prop);
    }

现在我从ember-data获得的错误是:

Assertion Failed: Ember Data expected a number or string to represent the record(s) in the `categories` relationship instead it found an object

0 个答案:

没有答案