用流星铁路由器中止导航

opt*_*ude 7 meteor iron-router

我在Meteor应用程序中有一个(客户端)路由器,并使用{{pathFor}}帮助程序进行链接.

我在用户更改表单字段时设置了一个dirty标志Session,并且我想触发警告并允许用户在设置标志时停止从页面导航,基本上就像onunload处理程序一样.

我试过这样做:

Router.onBeforeAction(function(pause) {
    var self = this;

    if (!this.ready()) {
        return;
    }

    if(Session.get('dirty')) {
        if(!confirm("Are you sure you want to navigate away?")) {
            pause();
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然而,当我得到提示时,我仍然被引导离开.也就是说,pause()似乎没有停止后续的路由器操作,无论它是什么.

我究竟做错了什么?

Dav*_*ave 6

据我所知,铁路由器API无法实现这一点.但你可以做的是覆盖Router.go方法,就像这样(在你的客户端代码中的某个地方):

var go = Router.go; // cache the original Router.go method
Router.go = function () {
  if(Session.get('dirty')) {
    if (confirm("Are you sure you want to navigate away?")) {
      go.apply(this, arguments);
    }
  } else {
    go.apply(this, arguments);
  }
};
Run Code Online (Sandbox Code Playgroud)


Chr*_*rra 0

您想去某个特定的地方吗?还有 Router.go(routeName) 将使页面指向给定的routeName。我想要的是也许你可以强制路由器转到当前页面,从而忽略后退操作。