【问题标题】:EmberJS: how to know if route was called because the user clicked the Back buttonEmberJS:如何知道是否因为用户单击了“返回”按钮而调用了路由
【发布时间】:2014-08-09 08:32:51
【问题描述】:

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

【问题讨论】:

    标签: ember.js routes browser-history


    【解决方案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'}}
    

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

    【讨论】:

    • 这解决了我的问题,谢谢。但它创造了另一个。我有一个显示微调器的 books.loading 路线。它进入这个中间状态,直到模型钩子的承诺被履行。在调用 BooksAllRoute.setupController() 之前,我看到正在呈现 books.loading 模板。但是,一旦我调用 this.replaceWith('books'),books.loading 模板就不再显示,即使浏览器控制台显示它确实正确转换到了中间 'books.loading' 状态。调用 this.replaceWith('books') 后有什么方法可以恢复 'books.loading'?
    • 我将不得不尝试复制它,我想我以前从未见过它。
    • 所以我知道这可能与您当前的设置不同,但是您是否介意对 jsbin 进行最后的润色,如果我真的从已知点开始,追下去会更容易,@ 987654321@
    • 我创建了一个 jsbin 来尝试复制我的问题,但一切正常,所以你的建议没有错。我的应用肯定有其他问题。
    • 哈,是的,我认为这可能会发生,我不确定使用 replaceWith 在转换中间会发生什么,它显然没有完成转换,并替换了以前的历史记录(我在上面假设在 cmets 中)。这只是意味着您可以使用transitionTo 中间过渡,并且当前过渡将替换为新过渡。 jsbin.com/harusuxo/6/edit
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多