Skip to content

Commit

Permalink
[DOCS] Code examples for transitionTo, replaceWith and recogni… (#18179)
Browse files Browse the repository at this point in the history
[DOCS] Code examples for transitionTo, replaceWith and recognize methods of Router service
  • Loading branch information
rwjblue authored Jul 12, 2019
2 parents e568879 + d333059 commit 70d4b72
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ export default class RouterService extends Service {
This behavior is different from calling `transitionTo` on a route or `transitionToRoute` on a controller.
See the [Router Service RFC](https://github.com/emberjs/rfcs/blob/master/text/0095-router-service.md#query-parameter-semantics) for more info.
In the following example we use the Router service to navigate to a route with a
specific model from a Component.
```javascript
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
router: service(),
actions: {
goToComments(post) {
this.router.transitionTo('comments', post);
}
}
});
```
@method transitionTo
@param {String} routeNameOrUrl the name of the route or a URL
@param {...Object} models the model(s) or identifier(s) to be used while
Expand Down Expand Up @@ -123,6 +141,20 @@ export default class RouterService extends Service {
This behavior is different from calling `replaceWith` on a route.
See the [Router Service RFC](https://github.com/emberjs/rfcs/blob/master/text/0095-router-service.md#query-parameter-semantics) for more info.
Usage example:
```app/routes/application.js
import Route from '@ember/routing/route';
export default Route.extend({
beforeModel() {
if (!authorized()){
this.replaceWith('unauthorized');
}
}
});
```
@method replaceWith
@param {String} routeNameOrUrl the name of the route or a URL
@param {...Object} models the model(s) or identifier(s) to be used while
Expand Down Expand Up @@ -245,6 +277,27 @@ export default class RouterService extends Service {
by the URL. Returns `null` if the URL is not recognized. This method expects to
receive the actual URL as seen by the browser including the app's `rootURL`.
See [RouteInfo](/ember/release/classes/RouteInfo) for more info.
In the following example `recognize` is used to verify if a path belongs to our
application before transitioning to it.
```
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
router: service(),
path: '/',
click() {
if(this.router.recognize(this.path)) {
this.router.transitionTo(this.path);
}
}
});
```
@method recognize
@param {String} url
@public
Expand Down

0 comments on commit 70d4b72

Please sign in to comment.