Skip to content

Commit

Permalink
Removing route params since we dont really use them
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerac4 committed Apr 4, 2019
1 parent 90674c0 commit 28d38a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
17 changes: 8 additions & 9 deletions addon/decorators/controller-pagination.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -38,15 +38,15 @@ export default function controllerPagination<T extends ConcreteSubclass<any>>(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();
}

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);
Expand Down Expand Up @@ -109,9 +109,9 @@ export default function controllerPagination<T extends ConcreteSubclass<any>>(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;
}
Expand All @@ -122,7 +122,7 @@ export default function controllerPagination<T extends ConcreteSubclass<any>>(Co
}

clearSorting() {
this.sort = A();
this.set('sort', A());
}

@action
Expand All @@ -148,8 +148,7 @@ export default function controllerPagination<T extends ConcreteSubclass<any>>(Co

@action
clearModels() {
this.model = A();
// set(this, 'model', A());
this.set('model', A());
}

@action
Expand Down Expand Up @@ -178,7 +177,7 @@ export default function controllerPagination<T extends ConcreteSubclass<any>>(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();
}
Expand Down
11 changes: 5 additions & 6 deletions addon/mixins/controller-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -25,15 +25,15 @@ 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();
}

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);
Expand Down Expand Up @@ -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 [];
}
Expand Down
19 changes: 4 additions & 15 deletions addon/utils/query-params.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,26 +19,23 @@ 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
* @returns - Object with query params to send to server
*/
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);
}

/**
Expand Down

0 comments on commit 28d38a1

Please sign in to comment.