ember数据串行器数据映射 [英] ember data serializer data mapping

查看:89
本文介绍了ember数据串行器数据映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ember& ember-data从服务器尝试使用json feed。这是我的代码:

  App = Ember.Application.create(); 

DS.RESTAdapter.configure(
plurals,{
category:'categories'
}
);

App.Store = DS.Store.extend({
revision:12,
adapter:DS.RESTAdapter.create({
url:'app'
})
});

App.Router.map(function(){
this.resource('categories');
});

App.CategoriesRoute = Ember.Route.extend({
model:function(){
return App.Category.find();
}
});

var attr = DS.attr;

App.Category = DS.Model.extend({
name:attr('string')
});

现在这个测试服务器可以正常工作。
使用以下JSON

  {
类别:[
{
name:Beef,
id:1
},
{
name:Pork,
id
}
]
}

然而在生产中,服务器提供以下json:

  {
success:true,
message:请求成功,
total:2,
data:[
{
name:Beef,
id:1
},
{
name:Pork,
id:2
}
]
}

我不能为我的生活,如何使用序列化程序来使用活的json。任何帮助将不胜感激。感谢提前。



更新:



我以前尝试编写序列化程序,但它不似乎正在工作...



见下文

  Store = DS.Store.extend({
revision:12,
adapter:DS.RESTAdapter.create({
url:'app',
serializer:DS.RESTSerializer。扩展({
extract:function(loader,json,type,record){
var root ='data';
this.sideload(loader,type,json,root);
this.extractMeta(loader,type,json);
if(json [root]){
if(record){loader.updateId(record,json [root]);}
this.extractRecordRepresentation(loader,type,json [root]);
}
}
})
})
});

现在产生此错误未捕获错误:断言失败:您的服务器返回哈希与关键数据,但您没有映射

解决方案

您有2个选项




  • 使您的服务器兼容,并让它返回json作为余烬数据预期,

  • 写你的自己的适配器/序列化程序支持这种格式。



更新:编写自己的序列化程序
更新2:摆脱未使用的函数



https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L196



您可以从 DS.RESTSerializer 继承,并使用此代码更改 extract p>

  extract:function(loader,json,type,record) {
var root ='data';

if(json [root]){
if(record){loader.updateId(record,json [root]); }
this.extractRecordRepresentation(loader,type,json [root]);
}
}

这假设请求的内容将始终处于您的json的数据键。


I'm using ember & ember-data to try and consume a json feed from the server. Here is my code:

App = Ember.Application.create();

DS.RESTAdapter.configure(
    "plurals", {
        category: 'categories'
    }
);

App.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.RESTAdapter.create({
        url: 'app'
    })
});

App.Router.map(function(){
    this.resource('categories');
});

App.CategoriesRoute = Ember.Route.extend({
    model: function() {
        return App.Category.find();
    }
});

var attr = DS.attr;

App.Category = DS.Model.extend({
    name: attr('string')
});

Now this works fine with a testing server. Using the following JSON

{
    "categories":[
        {
            "name":"Beef",
            "id":1
        },
        {
            "name":"Pork",
            "id":2
        }
    ]
}

However in production the server provide the following json:

{
    "success":true,
    "message":"Request successful",
    "total":2,
    "data":[
        {
            "name":"Beef",
            "id":1
        },
        {
            "name":"Pork",
            "id":2
        }
    ]
}

I can't for the life of me work out how to use the serializer to consume the live json. Any help would be appreciated. Thanks in advance.

UPDATE:

I've since tried to write the serializer but it doesn't appear to be working...

see below

App.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.RESTAdapter.create({
        url: 'app',
        serializer: DS.RESTSerializer.extend({
            extract: function(loader, json, type, record) {
                var root = 'data';
                this.sideload(loader, type, json, root);
                this.extractMeta(loader, type, json);
                if (json[root]) {
                    if (record) { loader.updateId(record, json[root]); }
                    this.extractRecordRepresentation(loader, type, json[root]);
                }
            }
        })
    })
});

Which now produces this error Uncaught Error: assertion failed: Your server returned a hash with the key data but you have no mapping for it

解决方案

You have 2 options

  • make your server compatible, and let it returns the json as ember data expects it,
  • write your own adapter/serializer to support this format.

UPDATE: write your own serializer UPDATE 2: get rid of unused functions

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L196

You can inherit from the DS.RESTSerializer and change extract with this code

  extract: function(loader, json, type, record) {
    var root = 'data';

    if (json[root]) {
      if (record) { loader.updateId(record, json[root]); }
      this.extractRecordRepresentation(loader, type, json[root]);
    }
  }

This assumes that the content of request will always be under the data key of your json.

这篇关于ember数据串行器数据映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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