0

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 Answers 2

0

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

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')
5
  • But peek all will not make any network request. It will fetch data from data store only. What I have did currently is using store.query to fetch data from rest service and populate data store and after that using peekAll to fetch live record. But I am not sure whether this is the right approach. Jun 14, 2016 at 1:55
  • @ManuBenjamin this doesn't access the meta (which was your question) but it is an example of a live record array that shows the current number of comments in the store as you load more from a server.
    – averydev
    Jun 14, 2016 at 20:48
  • I will not be fetching all records in single query, but I will get the total records that present in the db as meta info in rest response. So what I need is the total comments in db not the count of comments in the store. Jun 15, 2016 at 3:03
  • Personally I would likely just make it a separate request to get the information about the data. But your question was how to access the metadata, which it looks like you were able to access (pretty neat).
    – averydev
    Jun 15, 2016 at 16:31
  • Yes @averydev..we can use store.typeMapFor to get meta data from find all response. Intially i didn't found this solution. Some recomended using store.query, after using that i got meta data(respose.get('meta')), but records return is not live records like we get from findAll. I guess you got my issue. Jun 16, 2016 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.