Skip to content

Instantly share code, notes, and snippets.

@TylerJPresley
Created August 11, 2016 14:43
Show Gist options
  • Save TylerJPresley/e43117817aea23dba16a69419602dfec to your computer and use it in GitHub Desktop.
Save TylerJPresley/e43117817aea23dba16a69419602dfec to your computer and use it in GitHub Desktop.
Handling params // Aurelia (RequireJS/CLI)
...
<pagination url="/widgets" pagination.bind="pagination" params.bind="paginationParams"></pagination>
...
...
@computedFrom('searchWidget', 'searchGroups', 'searchText', 'searchType')
get paginationParams() {
if (!this.searchWidget && !this.searchGroups && !this.searchText && !this.searchType) { return null; }
return {
searchWidget: this.searchWidget,
searchGroups: this.searchGroups,
searchText: this.searchText,
searchType: this.searchType
};
}
...
activate(params, routeConfig, navigationInstruction) {
/** Sets the page title */
routeConfig.navModel.setTitle(this.heading);
this.page = params.page;
this.searchWidget = params.searchWidget;
this.searchGroups = params.searchGroups;
this.searchText = params.searchText;
this.searchType = params.searchType;
if (this.searchWidget || this.searchGroups || this.searchText || this.searchType) {
this.showSearch = true;
}
/** Call the exercise and recommendation service methods */
return this.widgetService.getStuff(this.page, this.searchText, this.searchType, this.searchWidget, this.searchGroups)
.then(response => {
if (response && response.model) {
this.pagination = response.meta.pagination;
this.model = response.model;
}
});
}
...
search() {
let params = Utility.convertObjectToQueryString(this.paginationParams);
this.router.navigate(`widgets${ params ? '?' + params : '' }`);
}
...
...
@bindable({ name: 'pagination', defaultValue: {}, defaultBindingMode: bindingMode.oneWay })
...
goToPage(pageNumber) {
let params = Utility.convertObjectToQueryString(this.params);
this.router.navigate(`${this.url}?page=${pageNumber}${ params ? '&' + params : '' }`);
}
...
/**
* Converts an object to querystring params
* @param obj
* @returns {string}
*/
static convertObjectToQueryString(obj) {
if (obj) {
let parts = [];
for (let key in obj) {
if (obj.hasOwnProperty(key) && obj[key] && obj[key] !== '') {
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`);
}
}
return parts.join('&');
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment