Map JSON array of object ids to model?

Hey! I am doing a simple search of posts, and I am having trouble rendering model, I am receiving this from server side (array of post ids, and the post models

 posts: [{ id:1,...}, {id:2,...}]                   
            
 searches:[ {
              posts:[1,2,3,...]
                  } ]

I have a model search, that i use to send the input search text and retrieve the post ids:

 import DS from 'ember-data';

 var attr= DS.attr;

export default DS.Model.extend({
 word: attr('string'),
 category: attr('string'),
 posts: attr()
});

I know it has something to do with overriding the serializer, but I am not sure, I don’t use relationships either, so is there some way I can map the array of ids to the post model and render the data in template?? Thanks!

Maybe like this, assume the variable search is your search model

posts: Ember.computed('search.posts', function () {
  var posts = this.get('search.posts'),
       args = {};
  args.promise = Ember.RSVP.all(posts.map((id) => this.store.findRecord('post', id)));

  return Ember.ArrayProxy.extend(Ember.PromiseProxyMixin).create(args);
})
```
1 Like

Thank u for your answer! I am pretty new to ember, so this might seem obvious, the computed property “posts:” should be in the model hook of the route, right?

put it on the controller that back up your template. :grinning: , you need to alias search to model, or replace search with model. to alias it use computed.alias.

You just need to return search model in model hook of the route.

1 Like