【问题标题】:meteor iron router execute action only if path changed流星铁路由器仅在路径更改时执行操作
【发布时间】:2014-09-06 15:43:12
【问题描述】:

我希望只有在我的路径改变时才执行钩子前的 Iron-router。

我的标题(布局)中有一个关键字搜索字段,当我更改页面时我会重置此字段,但当我停留在同一页面并触发一些事件时不会重置。

我该怎么做?

这就是我所拥有的:

Router.onBeforeAction ->
  $(".notification").remove()
  Session.set("keyword_search","")
  $("#keyword-search").val("")
  return

即使我点击了一个不改变路线的按钮,onBeforeAction 钩子也会被执行。这不是我想要的……

我尝试像这样使用 document.referrer

Router.onBeforeAction ->
  $(".notification").remove()
  if document.referrer.indexOf @path == -1  #only if new path
    Session.set("keyword_search","")
    $("#keyword-search").val("")

但这不起作用,因为当我单击页面按钮时,引荐来源网址仍然是具有不同 url 的最后一页,并且我的 keyword_search 字段被重置。

有什么想法吗?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    为什么不将路径存储在 Session 变量中并检查它是否已更改?从您的第二个代码块:

    Router.onBeforeAction ->
      $(".notification").remove()
      unless Session.equals("current_path", @path) # only if new path
        Session.set("current_path", @path)
        Session.set("keyword_search", "")
        $("#keyword-search").val("")
    

    【讨论】:

    • 我尝试使用会话变量,但在设置会话变量时它正在重新计算内容,并且最终电流始终等于先前的路径
    【解决方案2】:

    这里的解决方案是使用 onRun 钩子。

    onRun 会在每次路由更改时触发(而不是像我想的那样在每次路由器启动时触发......)。

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2016-01-22
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2014-09-07
      相关资源
      最近更新 更多