Ember-Data:访问侧面资源列表? [英] Ember-Data: Accessing a list of sideloaded resources?

查看:73
本文介绍了Ember-Data:访问侧面资源列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JSON在 / documents 路径(ID是UUID)中具有此结构:

I have some JSON that has this structure in the /documents path (the IDs are UUIDs):

{
   "tags": [
      {
         "id": "a33fc396-2428-11e3-8eeb-0800270f33f4",
         "name": "test"
      }
      <more tags not shown>
   ],
   "documents": [
      {
         "id": "c41460fa-2427-11e3-8702-0800270f33f4",
         "name": "file.txt",
         "tag_ids": [
            "a33fc396-2428-11e3-8eeb-0800270f33f4"
         ]
      }
      <more documents not shown>
   ]
}

我们看到标签资源是侧面加载的。我正在使用ember数据来使用这些路由加载JSON:

We see that the Tag resource is sideloaded. I'm using ember-data to load the JSON using these routes:

App.Router.reopen
  location: 'history'
  rootURL: '/'

App.Router.map ->
  @resource 'documents', ->

App.DocumentsRoute = Ember.Route.extend
  model: ->
    @get('store').findAll('document')

和模型:

App.Document = DS.Model.extend
  name: DS.attr('string')
  tags: DS.hasMany('tag')

App.Tag = DS.Model.extend
  name: DS.attr('string')

这工作正常;我可以通过我的模板中的句柄 {{#each}} 阻止访问所有文档,我可以验证我可以访问所有属于给个人文档。

This works fine; I can access all of the documents through a handlebars {{#each}} block inside my templates, and I can verify that I can access all tags belonging to a given individual document.

但是,我也想访问所有标签的列表,而不必在同一个模板中进入每个文档。它不应该是困难的,因为它在JSON中,作为一个侧面的资源,对吧?除了我不知道如何做到这一点。我已经在控制台中输入了各种各样的东西,看看它是否属于控制器中的一个属性,我还没有找到任何有希望的东西。我猜想我需要加载它并将其设置为我的控制器中的某个东西,但是我不知道如何编写它。为了能够写这样的东西,我需要添加什么?

However, I would also like to have access to the list of all tags, without going into each document, in the same template. It shouldn't be hard, because it's there in the JSON, as a sideloaded resource, right? Except I can't figure out how to do it. I've typed all sorts of things into the console to see if it's in one of the attributes within the controller, and I haven't found anything promising. I'm guessing I need to load it and set it to something in my controller, but I don't know how to write it. What do I need to add to my code in order to be able to write something like this?

{{#each tags}}
  Name: {{name}} <--- should print "test"
{{/each}}

任何想法都赞赏!

推荐答案

因为你已经加载了所有标签,而你不想向 / tags 发送另一个请求。您可以使用 store.all('tags')获取已加载的标签:

Because you already loaded all tags, and you don't want to send another request to /tags. You can use store.all('tags'), to get the already loaded tags:

App.DocumentsRoute = Ember.Route.extend({
    model: function() {
        var store = this.store;
        return store.findAll('document').then(function(documents) {
            // return an object with documents and tags, to be able to use both in the template
            return {
                documents: documents,
                tags: store.all('tag') // to access all tags loaded in the payload we can just use store.all, so no additional request will be sent
            }
        });        
    }
});

在您的模板中:

{{#each documents}}
  {{#each tags}}
    Tags of that document
  {{/each}}
{{/each}}

{{#each tags}}
  All tags available
{{/each}}

您可以在该小提琴中看到这个动作。 http: //jsfiddle.net/marciojunior/v4aZj/

You can see this in action in that fiddle http://jsfiddle.net/marciojunior/v4aZj/

观察

在您的有效载荷中,如果您使用的是 tag_ids ,则只需使用 ActiveModelAdapter $ c> RESTAdapter 您需要更改为标签

In your payload you have tag_ids this just work out of the box with ActiveModelAdapter if you are using RESTAdapter you need to change to tags.

这篇关于Ember-Data:访问侧面资源列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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