Ember.js 余烬数据:访问侧载资源列表?

Ember.js 余烬数据:访问侧载资源列表?,ember.js,ember-data,Ember.js,Ember Data,我有一些JSON在/documents路径中具有这种结构(ID是UUID): 和型号: App.Document = DS.Model.extend name: DS.attr('string') tags: DS.hasMany('tag') App.Tag = DS.Model.extend name: DS.attr('string') 这很好用;我可以通过模板内的把手{{{{each}}块访问所有文档,并且我可以验证是否可以访问属于给定单个文档的所有标记 但是,我也希望能

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

和型号:

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

App.Tag = DS.Model.extend
  name: DS.attr('string')
这很好用;我可以通过模板内的把手
{{{{each}}
块访问所有文档,并且我可以验证是否可以访问属于给定单个文档的所有标记

但是,我也希望能够访问所有标签的列表,而不必进入同一模板中的每个文档。这应该不难,因为它在JSON中,作为一个侧载资源,对吗?除了我不知道怎么做。我在控制台中键入了各种内容,以查看它是否在控制器中的某个属性中,但我没有发现任何有希望的内容。我猜我需要加载它并在控制器中设置它,但我不知道如何编写它。我需要向代码中添加什么才能编写这样的代码

{{#each tags}}
  Name: {{name}} <--- should print "test"
{{/each}}
{{{#每个标签}

名称:{{Name}}因为您已经加载了所有标记,并且不想向
/tags
发送另一个请求。您可以使用
store.all('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}}
你可以在小提琴上看到这一点

观察


在您的有效负载中,您有
tag\u id
这只需使用
ActiveModelAdapter
即可运行,如果您正在使用
RESTAdapter
您需要更改为
tags

,这正是我想要的。非常感谢!另外,是的,我正在使用
ActiveModelAdapter
:)@Marcio Junior。冷静的回答!如果我使用这种方法,那么我必须从我的控制器中引用标签和文档,比如
this.get(“model.tags”)
this.get(“model.documents”)
。@steakchaser你可以使用
this.get('model.documents')
或者只使用
this.get('documents')
。最终实现了这个功能。我想我应该知道这一点,但如果你真的定义了一个控制器,并希望它的模型是一个对象,你需要将它声明为
ObjectController
。嗯。感谢@MarcioJunior提供的信息。这是一个更新的小提琴,显示了以下内容:
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}}