【问题标题】:Nested Routing Behavior for Ember.jsEmber.js 的嵌套路由行为
【发布时间】:2013-07-20 18:16:02
【问题描述】:

有人可以解释 Ember.js 中路由器和嵌套路由的行为吗?

生成的 URL、RouteName、Controller、Route 和 Template 是什么?

App.Router.map(function(){
    this.resource('profile');

    // URL: /profile
    // RouteName: profile
    // Controller: ProfileController
    // Route: ProfileRoute
    // Template: profile

    this.resource('artists', function(){

        // URL: /artists
        // RouteName: artists OR artists.index
        // Controller: ArtistsController OR ArtistsIndexController
        // Route: ArtistsRoute OR ArtistsIndexRoute
        // Template: artists OR artists/index

        this.resource('artists.artist', { path: ':artist_id' }, function(){

            // URL: /artists/:artist_id
            // RouteName: artists.index OR artist.index
            // Controller: ArtistsIndexController OR ArtistIndexController
            // Route: ArtistsIndexRoute OR ArtistIndexRoute
            // Template: artists/index OR artist/index

            this.resource('artist.tracks', function(){

                // URL: /artists/:artist_id/tracks
                // RouteName: artists.tracks OR artists.artist.tracks OR artist.tracks
                // Controller: ArtistsTracksController OR ArtistsArtistTracksController OR ArtistTracksController
                // Route: ArtistsTracksRoute OR ArtistsArtistTracksRoute OR ArtistTracksRoute
                // Template: artists/tracks OR artists/artist/tracks OR artist/tracks

                this.route('playing', { path: ':track_id' });

                    // URL: /artists/:artist_id/tracks/:track_id
                    // RouteName: tracks.index
                    // Controller: TracksIndexController
                    // Route: TracksIndexRouteRoute
                    // Template: tracks/index
            });
        });
    });
});

如果您想查看我的 github 上的所有代码https://github.com/Gerst20051/HnS-Wave/tree/master/src/stations

来自我的 github 的 JavaScript 文件 https://github.com/Gerst20051/HnS-Wave/blob/master/src/stations/js/app.js

本指南是我引用的http://emberjs.com/guides/routing/defining-your-routes/

我从这个https://github.com/inkredabull/sonific8tr复制了我的应用程序结构

非常感谢我和整个 emberjs 社区在 emberjs 奋斗巴士上的帮助!

【问题讨论】:

    标签: javascript ember.js url-routing ember-router


    【解决方案1】:

    您需要删除嵌套路由的点表示法。只使用artist 而不是artists.artist

    你对应的路由器是,

    App.Router.map(function() {
      this.resource('profile');
      this.resource('artists', function() {
        this.resource('artist', { path: ':artist_id'}, function() {
          this.resource('tracks', function() {
            this.resource('playing', { path: ':track_id' });
          })
        });
      });
    });
    

    您可以使用App.Router.router.recognizer.names 获取路由器中映射的路由列表。

    这将为您提供以下 URL、路由和控制器。

    • /profile - ProfileRoute - ProfileController
    • /artists - ArtistsRou​​te - ArtistsController
    • /artists/1 - ArtistRoute - ArtistController
    • /artists/1/tracks - TracksRoute - TracksController
    • /artists/1/tracks/1 - PlayingRoute - PlayingController

    另外请注意,每个具有嵌套资源的资源也会获得一个隐式索引路由。例如:- ArtistsIndexRoute、ArtistIndexRoute、TracksIndexRoute,但不是 PlayingIndexRoute,因为它没有嵌套路由。

    【讨论】:

    • 感谢darshan的及时回复!我明天会继续努力,让你知道进展如何。
    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多