使用Ember数据获取非常规JSON [英] GET unconventional JSON with Ember-data

查看:79
本文介绍了使用Ember数据获取非常规JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ember 1.5.1和Ember-data 1.0 beta,我正在使用DS.RESTADAPTER CLASS。
我有两个模型,让我们说发布用户。服务器以GET请求回复以下JSON

  {
data:[....]
}

数据是一个数组用户帖子取决于请求。



RestAdapter是围绕着与服务器交换的JSON应该是传统的想法而设计的,它期望从服务器返回的JSON应该像这样

  {
posts:[....]
}

  {
用户:[....]
}

取决于请求。



如何自定义ember数据处理这种情况?

解决方案

我能够通过定制 extractArray 方法

  //覆盖extractArray方法
App.PostSer ializer = DS.RESTSerializer.extend({
extractArray:function(store,type,payload,id,requestType){
var myposts = payload.data;
var newpayload = {posts:myposts};
return this._super(store,type,newpayload,id,requestType);
}
});

以下资源非常有用:



https://github.com $ / $ / .RESTSerializer.html#method_extractArrayrel =nofollow> http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray


I am working with Ember 1.5.1 and Ember-data 1.0 beta and I am using the DS.RESTADAPTER CLASS. I have two models, let say Post and User. The server replies at a GET request with the following JSON

{
  data: [ .... ]
}

data is an array of users or posts depending on the request.

The RestAdapter is designed around the idea that the JSON exchanged with the server should be conventional, and it expects the JSON returned from your server should look like this

{
  posts: [ .... ]
}

or

{
  users: [ .... ]
}

depending on the request.

How to customize ember-data to handle such situation?

解决方案

I was able to handle the situation described on the above question by means of a customization of the extractArray method

// override extractArray method 
App.PostSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, id, requestType) {
    var myposts = payload.data;
    var newpayload = { posts: myposts };
    return this._super(store, type, newpayload, id, requestType);
  }
});

The following resources have been very helpful:

https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray

这篇关于使用Ember数据获取非常规JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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