Multiple Dynamic Segments in a Single Resource in Ember.js

前端 未结 3 1672
抹茶落季
抹茶落季 2021-01-12 19:21

Is there a way to have multiple dynamic segments with a single resource? My use case is to avoid letting the user hit index routes.

Example:

this.res         


        
3条回答
  •  悲哀的现实
    2021-01-12 20:10

    It would be better to use Ember's nested routes. Each route having its own dynamic segment.

    App.Router.map(function () {
        this.resource('albums', { path: '/albums' }, function () {
            this.resource('album', { path: ':album_id' }, function () {
                this.resource('tracks', { path: 'tracks' }, function () {
                    this.resource('track', { path: ':track_id' });
                });
            });
        });
    });
    

    If you want to show the user the first track immediately after clicking an album, you could use a redirect.

    App.AlbumRoute = Ember.Route.extend({
        afterModel: function (album, transition) {
            this.transitionTo('track', {album_id: album.id, track_id: album.tracks[0].id});
        },
    });
    

    Check out the docs on redirection: http://emberjs.com/guides/routing/redirection/

提交回复
热议问题