我在后端具有Rails的Backbone应用程序。假设你在页面http://localhost:3000/#home
然后去http://localhost:3000/#collection
。当用户点击后退按钮时,我希望它的行为与点击http://localhost:3000/#home
的链接完全相同,或者在地址栏中输入此网址并按Enter键。如何防止Back按钮在Backbone应用程序中重新加载页面
然而,是什么,而不是将发生在后退按钮在index.html中的<script>
标签的代码再次执行:
<script>
$(document).ready(function() {
AudioFileApp.initialize();
});
</script>
结果,不正确地创建自己的看法(比如他们如果你只需输入URL并按回车键)。我怀疑这个问题与turbolinks/Backbone/Rails问题有关,因为它解决了我在history already started
错误中遇到的问题:https://github.com/codebrew/backbone-rails/issues/134。但是,我仍然遇到了重新初始化的问题,作为初学者,我完全陷入了困境。
有没有办法确保Back的行为就像通过链接或地址栏访问上一页一样?
下面是实际的初始化方法是否有帮助:
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();
});
}