我需要知道我的路线是否被调用,因为用户点击了浏览器后退按钮,而不是点击{{link-to}}链接到达那里。这样做的原因是因为我有一个可过滤的列表,其中包含该路由的关联模板的分页。如果用户离开此列表然后单击后退按钮,我的设计人员希望过滤器的状态一致。但是,如果用户单击链接以到达此列表,我的设计人员希望重置过滤器和分页。
示例:
1) Users clicks the 'Books' navigation link and arrives at '/books'.
2) User filters the list goes to page 2 of results. This does not update the URL and user is still at '/books'.
3) User clicks one of the Books and goes to '/books/123'.
4) User clicks the browser Back button and goes back to '/books'. I need to show the filtered list at page 2.
5) User clicks the 'About Us' link and goes to '/about'
6) User clicks the 'Books' navigation link and arrives at '/books'. But this time, the user didn't click the browser Back button to arrive here, so I need to reset the filters and show a list of unfiltered Books on page 1.
在步骤4和步骤6中,用户到达' / books'和BooksRoute,但他们用来到那里的机制不同,所以我需要显示不同的书籍列表结果。有没有办法告诉是否因为单击“后退”按钮而未单击超链接而调用了路径?
答案 0 :(得分:1)
如果不在url中保存状态,我可能会创建一个虚拟路由,在通过该链接访问时消除books
状态。 (我假设控制器是你跟踪这种状态的地方)。
this.resource('books', function(){
this.route('all');
});
App.BooksAllRoute = Em.Route.extend({
setupController: function(){
var booksController = this.controllerFor('books');
booksController.set('filters', {});
// replaceWith replaces the history instead of adding additional history
// this may not be necessary since we're doing it mid transition
// this.transitionTo may be more appropriate
this.replaceWith('books');
}
});
{{link-to 'Books' 'books.all'}}
您可能需要将虚拟路径移动到路由器中的同一级别,以避免已经填充了书籍模型。 (我猜你是如何实际过滤你的结果的)