【发布时间】:2014-01-27 10:31:57
【问题描述】:
这个问题在 durandal 示例应用程序中很容易看到:
1) 导航到“模态对话框”演示 2) 点击按钮显示模态 3) 点击浏览器返回按钮
执行此操作时,页面导航到上一个屏幕,同时保持对话框打开,这几乎不是您想要的。
这里是否有关闭对话框或在打开对话框时阻止导航的最佳做法?
【问题讨论】:
标签: durandal
这个问题在 durandal 示例应用程序中很容易看到:
1) 导航到“模态对话框”演示 2) 点击按钮显示模态 3) 点击浏览器返回按钮
执行此操作时,页面导航到上一个屏幕,同时保持对话框打开,这几乎不是您想要的。
这里是否有关闭对话框或在打开对话框时阻止导航的最佳做法?
【问题讨论】:
标签: durandal
您可以绑定到 canDeactivate 回调。检查模态是否打开并阻止它,或者如果他们想离开,关闭它
【讨论】:
虽然我不希望这个答案立即适用于那里的许多人,但我想把它扔在那里以防它对某人有所帮助。
我们使用打字稿来创建我们的视图模型,并且我们有一个基础视图模型来封装通用功能(以及公开数据服务、发布/订阅等)。在这个视图模型中,这种方法似乎效果很好:
public ShowDialog(viewModel: any) {
// create a subscription to the router so that we can forcibly close the dialog in the event of a routing call
var subscription = router.on('router:route:activating');
subscription.then(() => {
// we will hit this block if the router is activating while this dialog is open
var moduleId = viewModel.__moduleId__ || '[No Id]';
system.log('Navigation detected while dialog is open - the dialog will now be forcibly closed. (' + moduleId + ')', viewModel);
dialog.close(viewModel);
});
var promise = App.showDialog(viewModel);
promise.then(() => {
subscription.off(); // kill the router subscription since the dialog is closed at this point
});
return promise;
}
消费者可以使用此功能直接替代 durandals 的对话框来选择启用自动关闭功能。
【讨论】: