简体   繁体   English

Ember.js中单个资源中的多个动态段

[英]Multiple Dynamic Segments in a Single Resource in Ember.js

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.resource('tracks', { path: 'albums/:album_id/tracks/:tracks_id' });

And I'd like to avoid the user from hitting the following routes: 我想避免用户点击以下路线:

albums/:album_id
albums/:album_id/tracks
albums/:album_id/tracks/:track_id

Routes: 路线:

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' });
    });
  });
});

Any help would be greatly appreciated. 任何帮助将不胜感激。

Defining Your Routes 定义你的路线

NOTE: If you define a resource using this.resource and do not supply a function, then the implicit resource.index route is not created. 注意:如果使用this.resource定义资源但不提供函数,则不会创建隐式resource.index路由。

It would be better to use Ember's nested routes. 使用Ember的嵌套路线会更好。 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/ 查看有关重定向的文档: http//emberjs.com/guides/routing/redirection/

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 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. 你可以在一个路径中定义多个slug并直接转到它,只需注意你只有一个模型用于该单一资源,所以你必须处理它。

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

A possible solution for us was to use the following: 我们可能的解决方案是使用以下内容:

App.AlbumsIndexRoute = Ember.Route.extend({ 
    redirect: function(){ 
        this.transitionTo('dashboard');
    } 
});    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM