Emberjs路由
App.Router.map(function() { this.resource('topCharts', function() { this.route('choose', { path: '/' }); this.route('albums'); this.route('songs'); this.route('artists'); this.route('playlists'); }); }); App.TopChartsChooseRoute = Ember.Route.extend({ beforeModel: function() { var lastFilter = this.controllerFor('application').get('lastFilter'); this.transitionTo('topCharts.' + (lastFilter || 'songs')); } }); // Superclass to be used by all of the filter routes below App.FilterRoute = Ember.Route.extend({ activate: function() { var controller = this.controllerFor('application'); controller.set('lastFilter', this.templateName); } }); App.TopChartsSongsRoute = App.FilterRoute.extend(); App.TopChartsAlbumsRoute = App.FilterRoute.extend(); App.TopChartsArtistsRoute = App.FilterRoute.extend(); App.TopChartsPlaylistsRoute = App.FilterRoute.extend();
model
钩子包含了基于承诺暂停过渡的许多应用场景,但是有的时候还是需要beforeModel
和afterModel
这两个钩子来提供帮助。最常见的原因是通过{{link-to}}
或者{{transitionTo}}
(最为URL改变导致的过渡的对比)过渡到一个具有动态URL端,这时将过渡到的路由的模型早早就被指定了(例如{{#link-to 'article' article}}
或者this.transitionTo('article', article)
),这种情况下,model
钩子并不会被调用。当路由正在收集所有路由的模型来执行过渡时,就需要使用beforeModel
和afterModel
钩子来处理所有逻辑。?