我一直在尝试为我的应用程序实现分页(我已经尝试了ember-cli-pagination和ember-simple-pagination),但是我遇到了很多问题。因此,我决定尝试自定义分页,并注意到我无法将参数传递到查询中。例如,在访问我的api时,http://jsonplaceholder.typicode.com/posts?_start=0&_limit=10会启动并限制两者均能正常工作。在我的路线中调用它时,它似乎完全忽略了它,只给了我所有条目。对于在这种情况下我做错了什么或如何正确实现分页,我将不胜感激。
app / adapters / post.js
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
host:'https://jsonplaceholder.typicode.com',
pathForType(){
return 'posts';
}
});
app / models / post.js
import DS from 'ember-data';
const { Model } = DS;
export default Model.extend({
user:DS.belongsTo('user'),
title:DS.attr('string'),
body:DS.attr('string'),
});
app / routes / post.js
import Route from '@ember/routing/route';
import { set } from '@ember/object';
import { hash } from 'rsvp';
export default Route.extend({
model() {
return hash({
post: this.store.query('post', {
start: 0,
limit: 10
}),
user: this.store.findAll('user')
});
},
setupController(controller, model) {
this._super(...arguments);
set(controller, 'posts', model.post);
set(controller, 'users', model.user);
}
});
答案 0 :(得分:1)
您需要在Route和Controller两侧都定义查询参数。
app / routes / post.js
import Route from '@ember/routing/route';
import { set } from '@ember/object';
import { hash } from 'rsvp';
export default Route.extend({
queryParams = {
start: {
refreshModel: true
},
limit: {
refreshModel: true
}
},
model() {
return hash({
post: this.store.query('post', {
start: 0,
limit: 10
}),
user: this.store.findAll('user')
});
},
setupController(controller, model) {
this._super(...arguments);
set(controller, 'posts', model.post);
set(controller, 'users', model.user);
}
});
在app / controllers / post.js内部
import Controller from '@ember/controller';
export default class ArticlesController extends Controller {
queryParams = ['start', 'limit'];
start = 1;
limit = 5;
}
默认情况下,更改查询参数后,Ember不会调用模型。我们通过 refreshModel:true 告诉它在开始/限制更改时执行此操作。