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

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

问题描述

我在 /documents 路径中有一些具有这种结构的 JSON(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-data 使用这些路由加载 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 开箱即用,如果您使用的是 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天全站免登陆