diff --git a/addon/decorators/controller-pagination.ts b/addon/decorators/controller-pagination.ts index 598760e..5b17312 100644 --- a/addon/decorators/controller-pagination.ts +++ b/addon/decorators/controller-pagination.ts @@ -1,5 +1,5 @@ import DS from 'ember-data'; -import { RouteParams, buildQueryParams, sortDirection } from 'gavant-pagination/utils/query-params'; +import { buildQueryParams, sortDirection } from 'gavant-pagination/utils/query-params'; import { tryInvoke } from '@ember/utils'; import { reject } from 'rsvp'; import { A } from '@ember/array'; @@ -38,7 +38,7 @@ export default function controllerPagination>(Co return Math.ceil(this.model.length / this.limit); } - async _loadModels(this: PaginationController, reset: boolean, params: RouteParams | undefined) { + async _loadModels(this: PaginationController, reset: boolean) { this.set('isLoadingPage', true); if(reset) { this.clearModels(); @@ -46,7 +46,7 @@ export default function controllerPagination>(Co const offset = this.offset; const limit = this.limit; - const queryParams = buildQueryParams(this, params, offset, limit); + const queryParams = buildQueryParams(this, offset, limit); let models = []; try { const result = await this.fetchModels(queryParams); @@ -109,9 +109,9 @@ export default function controllerPagination>(Co return this.store.query(modelName, queryParams); } - loadModels(reset: boolean = false, params?: RouteParams) { + loadModels(reset: boolean = false) { if (!this.isLoadingPage) { - return this._loadModels(reset, params); + return this._loadModels(reset); } else { return; } @@ -122,7 +122,7 @@ export default function controllerPagination>(Co } clearSorting() { - this.sort = A(); + this.set('sort', A()); } @action @@ -148,8 +148,7 @@ export default function controllerPagination>(Co @action clearModels() { - this.model = A(); - // set(this, 'model', A()); + this.set('model', A()); } @action @@ -178,7 +177,7 @@ export default function controllerPagination>(Co @action clearFilters() { - this.serverQueryParams.forEach((param: string) => this[param] = null); + this.serverQueryParams.forEach((param: string) => this.set(param, null)); // get(this, 'serverQueryParams').forEach((param) => set(this, param, null)); return this.filterModels(); } diff --git a/addon/mixins/controller-pagination.ts b/addon/mixins/controller-pagination.ts index ea25c90..52827cd 100644 --- a/addon/mixins/controller-pagination.ts +++ b/addon/mixins/controller-pagination.ts @@ -5,7 +5,7 @@ import { readOnly, or } from '@ember/object/computed'; import { tryInvoke } from '@ember/utils'; import { reject } from 'rsvp'; import { A } from '@ember/array'; -import { buildQueryParams, PaginationController, RouteParams, sortDirection } from 'gavant-pagination/utils/query-params'; +import { buildQueryParams, PaginationController, sortDirection } from 'gavant-pagination/utils/query-params'; import DS from 'ember-data'; export default Mixin.create({ @@ -25,7 +25,7 @@ export default Mixin.create({ return Math.ceil(get(this, 'model.length') / get(this, 'limit')); }), - async _loadModels(this: PaginationController, reset: boolean, params: RouteParams | undefined) { + async _loadModels(this: PaginationController, reset: boolean) { set(this, 'isLoadingPage', true); if(reset) { this.clearModels(); @@ -33,7 +33,7 @@ export default Mixin.create({ const offset = get(this, 'offset'); const limit = get(this, 'limit'); - const queryParams = buildQueryParams(this, params, offset, limit); + const queryParams = buildQueryParams(this, offset, limit); let models = []; try { const result = await this.fetchModels(queryParams); @@ -87,12 +87,11 @@ export default Mixin.create({ /** * Change the sorting and call `filterModels`. Will only load models if not currently making an API call * @param reset - Clear models - * @param params - Route params * @returns - an array of models */ - loadModels(this: PaginationController, reset: boolean, params: RouteParams) { + loadModels(this: PaginationController, reset: boolean) { if (!get(this, 'isLoadingPage')) { - return this._loadModels(reset, params); + return this._loadModels(reset); } else { return []; } diff --git a/addon/utils/query-params.ts b/addon/utils/query-params.ts index 2336a77..a4108f2 100644 --- a/addon/utils/query-params.ts +++ b/addon/utils/query-params.ts @@ -1,16 +1,8 @@ import { get, set, getWithDefault } from '@ember/object'; import { isArray } from '@ember/array'; import { isEmpty } from '@ember/utils'; -import { merge } from '@ember/polyfills'; -// import Controller from '@ember/controller'; import moment from 'moment'; -export interface RouteParams { - offset: number | undefined; - limit: number; - sort: string[]; -} - export interface PaginationController { offset: number | undefined; limit: number; @@ -27,7 +19,6 @@ export enum sortDirection { /** * Builds the query params to send to the server by taking the controller, route params, and paging data(`offset` & `limit`) * @param controller - The pagination controller instance - * @param routeParams - Route Params (`offset`, `limit`, `sort`) * @param offset - Offset provides a starting point for paging. i.e. offset of 0 and limit of 10 gives you the first 10 records. offset of 10 and limit of 10 gives the next 10 * @param limit - How many records to ruturn for one api call * @param queryParamListName - The name of the query params you want to use to page on the server @@ -35,18 +26,16 @@ export enum sortDirection { */ export function buildQueryParams( controller: PaginationController, - routeParams: RouteParams, offset: number = 0, limit: number = 10, queryParamListName: string = 'serverQueryParams' ) { - let params = routeParams || {}; let list: any = controller[queryParamListName]; let queryParams = getParamsObject(list, controller); - params = merge(queryParams, params); - params.offset = getWithDefault(controller, 'offset', offset); - params.limit = getWithDefault(controller, 'limit', limit); - return removeEmptyQueryParams(params); + queryParams.offset = getWithDefault(controller, 'offset', offset); + queryParams.limit = getWithDefault(controller, 'limit', limit); + queryParams.sort = getWithDefault(controller, 'sort', []); + return removeEmptyQueryParams(queryParams); } /**