Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Backbone.js路由器:在页面模式中隐藏,而不是在按下back时返回_Javascript_Backbone.js - Fatal编程技术网

Javascript Backbone.js路由器:在页面模式中隐藏,而不是在按下back时返回

Javascript Backbone.js路由器:在页面模式中隐藏,而不是在按下back时返回,javascript,backbone.js,Javascript,Backbone.js,我有一个基于backbone.js路由器的应用程序,使用默认的hashchange处理程序在页面之间移动。在一个页面上,有一个按钮触发一个模式框,如果用户按下返回键,我会关闭该框,而不是向用户返回历史记录中的页面 我尝试过几种不同的方法,但我对使用hashchange/popstate还不熟悉,所以我希望得到一些指导 添加额外的hashchange侦听器并试图阻止传播失败;两个侦听器都被执行了 打开对话框时添加对router.navigate(“模式”)的调用会向历史堆栈中添加一个附加条目,这

我有一个基于backbone.js路由器的应用程序,使用默认的hashchange处理程序在页面之间移动。在一个页面上,有一个按钮触发一个模式框,如果用户按下返回键,我会关闭该框,而不是向用户返回历史记录中的页面

我尝试过几种不同的方法,但我对使用hashchange/popstate还不熟悉,所以我希望得到一些指导

  • 添加额外的hashchange侦听器并试图阻止传播失败;两个侦听器都被执行了

  • 打开对话框时添加对router.navigate(“模式”)的调用会向历史堆栈中添加一个附加条目,这意味着返回可以正常工作,但它会导致原始页面再次路由/重新呈现,而不仅仅是关闭模式


谢谢你的帮助

听起来这里的问题不一定是如何隐藏模式,而是如何防止渲染/未更改的视图在路由到时重新渲染

解决此问题的一种方法是维护一个“currentView”,并在路由处理程序中,针对该视图进行测试,以查看是否需要为路由执行工作

也可以专门为模态视图保留一个类似的变量,在路线上始终首先关闭模态视图

// In some scope accessible to the router
var currentView, modalView; // or app.currentView, etc.

// extend your router's `navigate` to always close the modal
navigate: function (fragment, options) {
  modalView && modalView.close(); // or however you close your modal
  Backbone.history.navigate(fragment, options);
  return this;
}

// in your modal route(s)
//
// Disclaimer here: if your modal windows are part of the history,
// their routes should probably also indicate what should be
// rendered *under* the modal, e.g. `/someroute/modal`, etc.
// Otherwise, what happens if a browser steps "back" into a modal
// route via history, or a direct link?  You probably want something
// to render under the modal.
modal: function () {
  modalView = new ModalView(); // or change the modal's content, etc

  // given the above point, you may want to render the view under this
  // modal here:
  this.index(); // for example

  // then continue to show your modal
},

// then in your other routes, for example `index`:
index: function () {
  // test somehow to make sure that the view needs to be rendered
  if (currentView instanceof IndexView) return;

  currentView = new IndexView();
  // continue to render/show the currentView
}

使用了一个稍加修改的版本,但以路由器为目标是完美的解决方案。谢谢@BradyEmerson你是否可以发布修改后的解决方案?我遇到了同样的用例。感谢您的帮助。谢谢