使用流星铁路由器终止导航 [英] Aborting navigation with Meteor iron-router
问题描述
我在Meteor应用程序中有一个(客户端)路由器,并使用{{pathFor}}
助手进行链接.
I have a (client-side) router in a Meteor app, and links using the {{pathFor}}
helper.
我在用户更改表单字段时在Session
中设置dirty
标志,并且我想触发警告并允许用户停止导航(如果设置了该标志),基本上就像onunload
处理程序.
I am setting a dirty
flag in the Session
when the user changes a form field, and I want to trigger a warning and allow the user to stop navigating away from the page if the flag is set, basically like an onunload
handler.
我尝试用以下方法做到这一点:
I've tried to do this with:
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()
似乎并不会停止随后的路由器操作.
However, whilst I get the prompt, I'm still being navigated away. That is, the pause()
doesn't seem to stop the subsequent router action, whatever it is.
我在做什么错了?
推荐答案
据我所知,铁路由器API不可能做到这一点.但是,您可以做的是像这样(在您的客户端代码中的某个地方)覆盖Router.go方法:
From what I can tell this isn't possible with the iron-router API. What you could do however is override the Router.go method like so (somewhere in your client code):
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);
}
};
这篇关于使用流星铁路由器终止导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!