Ember-Data: Accessing a list of sideloaded resources?

前端 未结 1 901
孤独总比滥情好
孤独总比滥情好 2021-01-20 23:39

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

{
   \"tags\": [
      {
         \"id\": \"a33fc396-2428-11e3-         


        
相关标签:
1条回答
  • 2021-01-21 00:30

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

    And in your template:

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

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

    Observation

    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.

    0 讨论(0)
提交回复
热议问题