Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-API support #6

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addon/mixins/controller-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default function ControllerPaginationClass<T extends ConcreteSubclass<any
hasMore: boolean = true;
limit: number = 10;
isLoadingPage = false;
pagingRootKey = 'page';
filterRootKey = 'filter';

@or('isLoadingPage', 'isLoadingRoute') isLoadingModels!: boolean;
@readOnly('model.length') offset: number | undefined;
Expand Down
23 changes: 20 additions & 3 deletions addon/utils/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface PaginationController {
limit: number;
sort: string[];
modelName: string;
pagingRootKey: string | null;
filterRootKey: string | null;
[key: string]: any;
}

Expand All @@ -32,8 +34,15 @@ export function buildQueryParams(
) {
let list: any = controller[queryParamListName];
let queryParams = getParamsObject(list, controller);
queryParams.offset = getWithDefault(controller, 'offset', offset);
queryParams.limit = getWithDefault(controller, 'limit', limit);
let pagingRoot = queryParams;

if(controller.pagingRootKey) {
queryParams[controller.pagingRootKey] = {};
pagingRoot = queryParams[controller.pagingRootKey];
}

pagingRoot.offset = getWithDefault(controller, 'offset', offset);
pagingRoot.limit = getWithDefault(controller, 'limit', limit);
return removeEmptyQueryParams(queryParams);
}

Expand All @@ -45,6 +54,12 @@ export function buildQueryParams(
*/
export function getParamsObject(parameters: [], context: PaginationController) {
let params: any = {};
let filterRoot = params;

if(context.filterRootKey) {
params[context.filterRootKey] = {};
filterRoot = params[context.filterRootKey];
}

if(isArray(parameters)) {
parameters.forEach((param: string) => {
Expand All @@ -61,8 +76,10 @@ export function getParamsObject(parameters: [], context: PaginationController) {
let serverDateFormat = getWithDefault(context, 'serverDateFormat', 'YYYY-MM-DDTHH:mm:ss');
value = moment(value).format(serverDateFormat);
}
params[key] = value;
filterRoot[key] = value;
});

filterRoot = removeEmptyQueryParams(filterRoot);
}

if(!isEmpty(get(context, 'sort'))) {
Expand Down
6 changes: 4 additions & 2 deletions tests/dummy/app/controllers/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import Controller from '@ember/controller';
import ControllerPagination from '@gavant/ember-pagination/mixins/controller-pagination';

export default class Application extends ControllerPagination(Controller) {
// normal class body definition here

serverQueryParams = ['foo', 'bar', 'baz'];
foo = 123;
bar = true;
baz = null;
}

// DO NOT DELETE: this is how TypeScript knows how to look up your controllers.
Expand Down
5 changes: 0 additions & 5 deletions tests/dummy/app/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ export default class Application extends RoutePagination(Route) {
const params = this.getControllerParams();
return this.store.query('customer', params);
}

setupController(controller: any, model: any) {
console.log('This is a test');
super.setupController(controller, model);
}
}
Loading