0
votes

I have created a comment model and trying to fetch all comments records. But I need a meta info total comments which is getting as a separate attribute outside comments array.

I am using Ember store.query to fetch records from rest service(I tried store.findAll, but it is giving me only record array of comments in promise response. Is it possible to modify that?). I am getting the records with total comments(meta) while using store.query(), but that record array is not getting updated when we save new records.

After doing some analysis I found that we can use filter for loading the live record, but filter is now deprecated in Ember(Ember 2.5.1). From the documentation It is clear that we can use ember-data-filter for loading live record. But I am confused to use that addon(Mentioned like it has some memory leakage issue) and not sure whether I will get meta information from response. Is there is any other way to fetch live records with meta information from the response.

Anyone please suggest a solution

2

2 Answers

0
votes

After doing some analysis, I found a solution to access meta data using store.findAll(). We can use typeMapFor in the findAll response to get the meta info in the response

store.typeMapFor(response.type)

Full code below,

store.findAll("comment").then(function(response) {
    var meta = store.typeMapFor(response.type);

    // your meta info will be in meta.metadata
    // var totalComments = meta.metadata.totalComments;
});

And the response record array is liveRecords which will get updated automatically, if we save new records.

store.query("comment").then(function(response) {
    var meta = response.get("meta");
    // We will get meta like this but reponse record array is not a liveRecords
});

Response getting from store.query() is just a recordArray (not liveRecords) which will not get updated with new records

0
votes

If you want an array of all records that updates as new records are populated you can use peekAll which returns a live record array.

Added Code sample:

loadRecords: function (){
    this.set('allComments', store.peekAll('comment'));
    this.store.findAll('comment');
},

recordCount: Ember.computed.alias('allComments.length')