Created
August 11, 2016 14:43
-
-
Save TylerJPresley/e43117817aea23dba16a69419602dfec to your computer and use it in GitHub Desktop.
Handling params // Aurelia (RequireJS/CLI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<pagination url="/widgets" pagination.bind="pagination" params.bind="paginationParams"></pagination> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@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 : '' }`); | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@bindable({ name: 'pagination', defaultValue: {}, defaultBindingMode: bindingMode.oneWay }) | |
... | |
goToPage(pageNumber) { | |
let params = Utility.convertObjectToQueryString(this.params); | |
this.router.navigate(`${this.url}?page=${pageNumber}${ params ? '&' + params : '' }`); | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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