繁体   English   中英

如何防止Backbone应用程序中的“后退”按钮重新加载页面

[英]How to prevent Back button from reloading page in Backbone app

我有一个后端上有Rails的Backbone应用程序。 假设您在页面http://localhost:3000/#home ,然后转到http://localhost:3000/#collection 当用户单击“后退”按钮时,我希望它的行为就像单击指向http://localhost:3000/#home或在地址栏中键入此URL并按Enter一样。

但是,用“后退”按钮代替的是,再次执行index.html中<script>标记中的代码:

<script>
  $(document).ready(function () {
    AudioFileApp.initialize();
  });
</script>

结果,无法正确创建我的视图(就像您只是输入URL并按Enter键一样)。 我怀疑这个问题与这个turbolinks / Backbone / Rails问题有关,因为它解决了我的history already started错误: https : //github.com/codebrew/backbone-rails/issues/134 但是,我仍然在重新初始化方面遇到问题,作为一个初学者,我完全感到困惑。

有没有办法确保后退的行为就像通过链接或地址栏访问上一页一样?

这是实际的初始化方法(如果有帮助的话):

initialize: function() {
  var router = new AudioFileApp.Routers.Router({
    $rootEl: $('#main'),
    currentUser: new AudioFileApp.Models.User({ id: CURRENT_USER_ID })
  });

  var navbar = new AudioFileApp.Views.NavbarView();
  $('#navbar').html(navbar.render().$el);

  var audioPlayer = new AudioFileApp.Views.AudioPlayer();
  $('#audio-player').html(audioPlayer.render().$el);

  if (!Backbone.history.started) {
    Backbone.history.start();
  }

  // Listen for Back button click
  $(document).on('page:load', function () {
    Backbone.history.stop();
    Backbone.history.start();
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM