jeudi 22 octobre 2015

Posts and comments with ember data async

I have 2 models, a post model and a comment model. I would like to download the comments asynchronously, and it works fine with the following models.

//models/post.js
export default DS.Model.extend({
    date: attr('string'),
    user: belongsTo('user'),
    comments: hasMany('comment', {async: true})
});

//models/comment.js
export default DS.Model.extend({
    date: attr('string'),
    user: belongsTo('user'),
    value: attr('string')
});

Although sometimes, a post can have hundreds of comment ids, and I don't want to load all the comments in one shot, this can be too big, so I'd like to limit the request.

If I am doing the following, it will download all the comments at once.

this.store.findRecord('post', 1).then((post) => {
    post.get('comments'); //Will call the server to load all the comments
});

I'm not even able to access the ids to get them manually through the store, as when I call .get('comments') it loads them all.

I'm wondering what could be my best solution to avoid to load all the comments in one shot?

Thanks




Aucun commentaire:

Enregistrer un commentaire