vendredi 16 décembre 2016

How can I have a collection that contains only persisted Ember Data records, excluding records that live on the client-side and are not persisted?

Assuming I have 3 users in the backend:

let users = this.get('store').findAll('user'); 
console.log(users.get('length'));// => 3, e.g. [{id: 1}, {id: 2}, {id: 3}] 
this.get('store').createRecord('user');
console.log(users.get('length')); // => 4, e.g. [{id: 1}, {id: 2}, {id: 3}, **{id: null}**]   

How can I retrieve a list of only persisted records, i.e., only records that have an ID assigned to them?

I realize that I can manually filter records that has no ID whenever I need to consume persisted users, but that looks messy and counterintuitive.
Most importantly with a manual approach I can't keep one reference to the collection, like what I had in the users variable there, meaning the collection can't be computed, doesn't updates with new records, etc.
I skimmed through Ember's docs but I found nothing of use, have I missed something there?
How can I only get a collection of persisted records from Ember's store?




Aucun commentaire:

Enregistrer un commentaire