ember.js - EmberJS : how to know if route was called because the user clicked the Back button

标签 ember.js routes browser-history

我需要知道我的路线是否因为用户单击浏览器后退按钮而不是单击 {{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,但他们到达那里的机制不同,因此我需要显示不同的书籍列表结果。有没有办法判断是否因为单击“后退”按钮而不是单击超链接而调用了路由?

最佳答案

如果不在 url 中保存状态,我可能会创建一个虚拟路由,在通过该链接访问时消除 books 状态。 (我假设 Controller 是您跟踪此状态的地方)。

路由器

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

您可能需要将虚拟路由移动到路由器中的相同级别,以避免已经填充书籍模型。 (我猜你实际上是如何过滤结果的)

关于ember.js - EmberJS : how to know if route was called because the user clicked the Back button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295836/

相关文章:

javascript - 在 Backbone.history 中读取以前的 URL

ember.js - ember-data 海关序列化

javascript - 在单击按钮期间检测文本区域是否聚焦

c# - 网络 API 2 404 错误

node.js - express + Mongoose : async/await returning undefined

css - 实验性 chrome 历史扩展

javascript - Ember route.connections 在渲染错误事件时未定义

ember.js - Ember 数据有很多只加载在 canonicalState 属性中的记录

rspec - 使用 Rspec/Capybara 时找不到 Rails 路线,[Rails 3.1.1,Ruby 1.9.3]

javascript - backbone 和 history 推送状态