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.
示例:
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
路线:
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' });
});
});
});
任何帮助将不胜感激.
注意:如果您使用 this.resource 定义资源并且不提供函数,则不会创建隐式的 resource.index 路由.
NOTE: If you define a resource using this.resource and do not supply a function, then the implicit resource.index route is not created.
推荐答案
最好使用 Ember 的嵌套路由.每条路线都有自己的动态路段.
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});
},
});
查看有关重定向的文档:http://emberjs.com/guides/routing/redirection/
Check out the docs on redirection: http://emberjs.com/guides/routing/redirection/
这篇关于Ember.js 中单个资源中的多个动态段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!