Multiple Dynamic Segments in a Single Resource in Ember.js

前端 未结 3 1570
抹茶落季
抹茶落季 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:01

    Just for completeness sake, the index routes aren't necessary, they are just a freebie convenience if you define them, if you don't define them it won't go to them.

    http://emberjs.jsbin.com/eMofowUQ/1/edit

    And you can define multiple slugs in a single path and go directly to it, just note you'll only have a single model for that single resource, so you'll have to deal with that.

    http://emberjs.jsbin.com/eMofowUQ/2/edit

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2021-01-12 20:11

    A possible solution for us was to use the following:

    App.AlbumsIndexRoute = Ember.Route.extend({ 
        redirect: function(){ 
            this.transitionTo('dashboard');
        } 
    });    
    
    0 讨论(0)
提交回复
热议问题