I want to implement paging/filtering on ember data asynchronously.
This is my author
model:
export default DS.Model.extend({
user: DS.belongsTo('user'),
articles: DS.hasMany('article', { async: true }),
name: DS.attr('string'),
email: DS.attr('string')
});
route:
export default Ember.Route.extend({
model: function(params) {
return this.store.find('author', params.author_id);
}
});
controller:
export default Ember.ObjectController.extend({
popularArticles: function() {
return this.get('model.articles').filter({ tab: 'popular' });
}.property('model.articles')
});
Note that model has an articles
property with DS.hasMany('article', { async: true})
relationship.
If I use this property this request is made authors/1/articles
and its asynchronous.
This is fine until I need to make request like authors/1/articles?page=2
or authors/1/articles?tab="hot"
.
One possible approach is, as shown in the controller, I have a popularArticles
property that filters the model.articles
property, and will make the filtered request without loading all the articles.
How can I pass query parameters to asynchronously loaded relationships in ember data?
@peter-t I don know what example you search for but you can filter in a component like this:
ember-cli-blog/app/components/blog-posts.js
ember-cli-blog/app/templates/posts.hbs
In the controller you can do something like this:
This addon may help: https://github.com/mdehoog/ember-data-has-many-query.
Allows you to add query params to
has-many
relationships, eg:post.query('comments', { page: 1 });