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

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

问题描述

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

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!" }
  ]
}

从服务器获取数据是可行的,但不幸的是,我无法从我的 JSON 响应中找出可以访问元信息的位置.

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.

基于我对 ember-data 的 github 问题的研究,对元信息的支持似乎是通过提交实现1787bff.

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"])
   }
})

你们中的任何人都可以为我解释一下吗?我知道 Ember api 正在快速发展,但我确信我只是遗漏了一小部分,这实际上是可能的.

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.

推荐答案

我找到了一种使用 ember-data 从服务器响应中提取元信息的更简洁的方法.

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'
 });

此后,每次服务器发送带有分页对象的元属性时,该对象将被添加到所请求模型类的商店的 TypeMaps 属性中.

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.

例如以下响应:

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

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

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

你不能直接观察 store 的 TypeMaps-property,所​​以我向 PostsController 添加了一个计算属性来访问请求分页元信息:

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')
 });

我真的不认为这是元信息问题的一个很好的解决方案,但这是我能用 Ember-Data 提出的最好的解决方案.也许这在未来会更容易.

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天全站免登陆