访问在json服务器响应中传递的元信息 [英] Accessing meta information passed in a json server response

查看:73
本文介绍了访问在json服务器响应中传递的元信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ember-Data Rest-Adapter,从我的服务器返回的JSON看起来基本上就像 Active模型序列化器文档

  {
meta:{total:10},
posts:[
{title:Post 1,body:Hello! },
{title:Post 2,body:再见!
]
}

从服务器获取数据工作,但不幸的是我无法确定我可以从我的JSON响应中访问元信息。



根据我对ember-data的github问题的研究,对元信息的支持似乎实施,提交 1787bff



但即使有测试用例,我也无法弄清楚如何访问元信息。

  App.PostController = Ember.ArrayController.extend({
....
requestSearchData:function(searchParams){
posts = App.Post.find (searchParams);
this.set('content',posts);
//不知道如何访问meta [total]
//但是我想做某事像这样:
// this.set('totalCount',meta [total])
}
})

请问有人可以给我看看吗?我知道Ember api正在快速移动,但我确信我只是错过了一小部分,这实际上是可能的。

解决方案

我发现一个更清洁的方法来从服务器响应中提取ember数据的元信息。



我们必须告诉序列化程序需要哪些元信息这个案例分页):

  App.serializer = DS.RESTSerializer.create(); 

App.serializer.configure({pagination:'pagination'});

App.CustomAdapter = DS.RESTAdapter.extend({
serializer:App.serializer
});

App.Store = DS.Store.extend({
adapter:'App.CustomAdapter'
});之后,每次服务器发送一个带有分页对象的元属性时,这个对象将被添加到商店的 TypeMaps 属性。



例如,使用以下响应:

  {
'meta':{'pagination':{'page':1,'total':10}},
'posts':[
...
]
}

App.Post的 TypeMap -Model将在帖子加载后包含分页对象。



您不能直接观察到商店的TypeMaps属性,所以我将一个计算属性添加到PostsController访问请求分页元信息:

  App.PostsController = Ember.ArrayController.extend({
pagination :function(){
if(this.get('model.isLoaded')){
modelType = this.get('model.type');
this.get('store ').typeMapFor(modelType).metadata.pagination
}
} .property('model.isLoaded')
});

我真的不认为这是元信息问题的一个很好的解决方案,但这是最好的解决方案我可以想出Ember-Data。也许这将来会更容易。


I am using the Ember-Data Rest-Adapter and the JSON returned from my server looks basically like the one in the Active Model Serializers Documentation

{
  "meta": { "total": 10 },
  "posts": [
    { "title": "Post 1", "body": "Hello!" },
    { "title": "Post 2", "body": "Goodbye!" }
  ]
}

Fetching the data from the server works but unfortunately I am not able to figure out where I can access the meta information from my JSON response.

Based on my research in ember-data's github issue, support for meta information seems to be implemented with commit 1787bff.

But even with the test cases I was not able to figure out how to access the meta information.

App.PostController = Ember.ArrayController.extend({
   ....
   requestSearchData: function(searchParams){
      posts = App.Post.find(searchParams);
      this.set('content', posts);
      // don't know how to access meta["total"]
      // but I want to do something like this:
      // this.set('totalCount', meta["total"])
   }
})

Can anybody of you shed some light on this for me, please? I am aware that the Ember api is moving fast but I am sure I am just missing a small part and that this is actually possible.

解决方案

I found a cleaner approach for extracting meta information from the server response with ember-data.

We have to tell the serializer which meta-information to expect (in this case pagination):

 App.serializer = DS.RESTSerializer.create();

 App.serializer.configure({ pagination: 'pagination' });

 App.CustomAdapter = DS.RESTAdapter.extend({
   serializer: App.serializer
 });

 App.Store = DS.Store.extend({
   adapter: 'App.CustomAdapter'
 });

After that every time the server sends a meta-property with a pagination object this object will be added to the store's TypeMaps property for the requested Model-Class.

For example with the following response:

  {
    'meta': {'pagination': { 'page': 1, 'total': 10 } },
    'posts':[
      ...
    ]
  }

The TypeMap for the App.Post-Model would include the pagination object after the posts have loaded.

You can't observe the TypeMaps-property of the store directly so I added an computed property to the PostsController to have access to the requests pagination meta information:

 App.PostsController = Ember.ArrayController.extend({
    pagination: function () {
      if (this.get('model.isLoaded')) {
        modelType = this.get('model.type');
        this.get('store').typeMapFor(modelType).metadata.pagination
      }
    }.property('model.isLoaded')
 });

I really don't think that's a great solution to the meta information problem but this is the best solution I was able to come up with yet with Ember-Data. Maybe this will be easier in the future.

这篇关于访问在json服务器响应中传递的元信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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