|
| 1 | +# @emberx/router: Fast, advanced, flexible routing library for browsers and node.js |
| 2 | + |
| 3 | +This is a futuristic take on the router of ember.js, which is the most advanced and mature |
| 4 | +routing solution I've seen so far in frontend development. It supports named routes, queryParameters, |
| 5 | +dynamic link segments, redirections, promise-aware route-template render delayin for SSR, |
| 6 | +parent-children route relationships. The router is fully testable with `visit` and `currentURL` helpers |
| 7 | +for `@emberx/test-helpers`. |
| 8 | + |
| 9 | +```ts |
| 10 | +// in index.ts: |
| 11 | + |
| 12 | +import startRouter from './start-router'; |
| 13 | + |
| 14 | +const Router = startRouter(); |
| 15 | + |
| 16 | +export default Router.visit(`${document.location.pathname}/${document.location.search}`); |
| 17 | + |
| 18 | +// =================================================== |
| 19 | +// in ./start-router.ts: |
| 20 | + |
| 21 | +import Router from '@emberx/router'; |
| 22 | +import IndexRoute from './routes/index/route.ts'; |
| 23 | +import PostsRoute from './routes/posts/route.ts'; |
| 24 | +import PostsIndexRoute from './routes/posts/index/route.ts'; |
| 25 | +import PostsPostRoute from './routes/posts/post/route.ts'; |
| 26 | + |
| 27 | +Router.addServices({ |
| 28 | + intl: new LocaleService(), |
| 29 | +}); |
| 30 | + |
| 31 | +let router = Router.start([ |
| 32 | + { |
| 33 | + path: '/', |
| 34 | + name: 'index', |
| 35 | + route: IndexRoute |
| 36 | + }, |
| 37 | + { |
| 38 | + path: '/posts', |
| 39 | + name: 'posts', |
| 40 | + route: PostsRoute, |
| 41 | + indexRoute: PostsIndexRoute |
| 42 | + }, |
| 43 | + { |
| 44 | + path: '/posts/:slug', |
| 45 | + name: 'posts.post', |
| 46 | + route: PostsPostRoute |
| 47 | + }, |
| 48 | + { |
| 49 | + path: '/posts/:blog_post_id/comments', |
| 50 | + name: 'posts.post.comments', |
| 51 | + route: PostsPostCommentsRoute |
| 52 | + }, |
| 53 | +]); |
| 54 | + |
| 55 | +export default router; |
| 56 | + |
| 57 | +// =================================================== |
| 58 | +// in ./routes/index/route.ts: |
| 59 | + |
| 60 | +import { Route, hbs, service } from '@emberx/router'; |
| 61 | +import { WelcomeBanner } from './components/welcome-banner'; |
| 62 | +import translate from './helpers/translate'; |
| 63 | +import RSVP from 'rsvp'; |
| 64 | + |
| 65 | +export default class PostsPostRoute extends Route { |
| 66 | + @service intl; |
| 67 | + |
| 68 | + static model(params, transition) { |
| 69 | + return RSVP.hash({ comments: fetchComments() }); // NOTE: RSVP.hash() returns a promise that waits for promises inside the hash |
| 70 | + } |
| 71 | + |
| 72 | + static includes = { WelcomeBanner, translate }; |
| 73 | + |
| 74 | + static template = hbs` |
| 75 | + <WelcomeBanner /> |
| 76 | +
|
| 77 | + <h5>{{translate "post-page.welcome"}}</h5> |
| 78 | + <LinkTo @route="posts" @model={{11}} data-test-preview-link>All posts</LinkTo> |
| 79 | + <LinkTo @route="posts.post" @query={{hash preview=true}} @model={{11}} data-test-preview-reviewed-comments-link>View post 11</LinkTo> |
| 80 | + <LinkTo @route="posts.post" @query={{hash preview=true asUser=22}} @model={{12}} data-test-preview-reviewed-comments-link>View post 12 in preview mode by user 22</LinkTo> |
| 81 | + <LinkTo @route="posts.post" @query={{hash priviledge="admin"}} @model={{11}} data-test-preview-pending-comments-link>View this post in admin priviledge mode</LinkTo> |
| 82 | +
|
| 83 | + <LinkTo @route="posts.post.comments" @model={{11}} data-test-preview-all-comments-link>See comments in detail</LinkTo> |
| 84 | +
|
| 85 | + {{#each this.model.comments as |comment index|}} |
| 86 | + <div data-test-comment="{{index}}"> |
| 87 | + <p>{{comment.content}} | <span class="{{comment.status}}">{{comment.status}}</span></p> |
| 88 | + <p>{{comment.postedAt}}</p> |
| 89 | + </div> |
| 90 | + {{/each}} |
| 91 | +
|
| 92 | + {{yield}} <!-- PostsPostCommentRoute can use this route in its template and extend it thanks to yield --> |
| 93 | +
|
| 94 | + <button {{on "click" this.changeLocale}}>{{translate "buttons.change-locale"}}</button> |
| 95 | + `; |
| 96 | + |
| 97 | + @action |
| 98 | + async changeLocale() { |
| 99 | + await this.intl.changeLocaleToRandomLocale(); |
| 100 | + console.log('locale changed to:', this.intl.currentLocale); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
0 commit comments