我在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(); } } });
然而,当我得到提示时,我仍然被引导离开.也就是说,pause()
似乎没有停止后续的路由器操作,无论它是什么.
我究竟做错了什么?
据我所知,铁路由器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);
}
};