1.0.0 • Published 3 years ago
ember-routing-utils v1.0.0
ember-routing-utils
Some utils for working with the Ember RouterService more effectively.
Installation
ember install ember-routing-utilsUsage
routing-utils Service
import { service, type DIRegistry } from '@ember/owner';
class {
@service router: DIRegistry['service']['router'];
@service routingUtils: DIRegistry['service']['routing-utils'];
// ...
}getParameters
getParameters(routeInfo: RouteInfo): string[]Retrieves all parameters for a RouteInfo object and its parents in correct
order, so that you can pass them to e.g. transitionTo(routeName, ...params).
const { currentRoute, currentRouteName } = this.router;
const params = this.routingUtils.getParameters(currentRoute);
this.router.transitionTo(currentRouteName, ...params, currentRoute.queryParams);getParametersWithQueryParameters
getParametersWithQueryParameters(routeInfo: RouteInfo): string[]Same as getParameters, but also includes the final { queryParams } for
convenience.
const { currentRoute, currentRouteName } = this.router;
const params = this.routingUtils.getParametersWithQueryParameters(currentRoute);
this.router.transitionTo(currentRouteName, ...params);getURLFromRouteInfo
getURLFromRouteInfo(routeInfo: RouteInfo): stringBuilds the URL for a RouteInfo object and its parents. Includes the rootURL.
const { currentRoute, currentRouteName } = this.router;
const url = this.routingUtils.getURLFromRouteInfo(currentRoute);removeRootURL
removeRootURL(url: string): stringRemoves the rootURL from a URL, so that it can be used with transitionTo(),
because Ember handles this inconsistently. 🤡
If the URL does not start with the rootURL or the app has no rootURL, this
just returns the original url.
prefixRootURL
prefixRootURL(url: string): stringPrefixes the rootURL to a URL.
If the URL already starts with the rootURL or the app has no rootURL, this
just returns the original url.