问题描述
这是我的作者
模型:
我想要实现对异常数据的分页/过滤。 / p>
导出默认DS.Model.extend({
用户:DS.belongsTo('user'),
文章:DS.hasMany('article',{async:true}),
名称:DS.attr('string'),
电子邮件:DS.attr('string')
});
路线:
code> export default Ember.Route.extend({
model:function(params){
return this.store.find('author',params.author_id);
}
});
控制器:
code> export default Ember.ObjectController.extend({
popularArticles:function(){
return this.get('model.articles')。filter({tab:'popular'}) ;
} .property('model.articles')
});
请注意,模型有一个文章
属性 DS.hasMany('article',{async:true})
关系。
如果我使用此属性请求是 authors / 1 / articles
及其异步的。
这是很好,直到我需要提出请求 author / 1 / articles?page = 2
或 authors / 1 / articles?tab =hot
p>
一个可能的方法是,如控制器所示,我有一个 popularArticles
属性过滤 model.articles
属性,并将过滤请求,而不加载所有文章。
如何将查询参数传递给异步加载的关系在ember数据?
此插件可能有帮助:。
允许您将查询参数添加到 has-many
关系,例如:
post.query('comments',{page:1});
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?
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 });
这篇关于过滤器分页传递参数到异步ember数据关系请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!