如何防止Backbone按钮重新加载Backbone app中的页面

时间:2015-06-25 04:13:57

标签: javascript ruby-on-rails backbone.js

我在后端有一个带Rails的Backbone应用程序。假设您在页面http://localhost:3000/#collection上,然后转到http://localhost:3000/#home。当用户单击“返回”按钮时,我希望其行为与单击指向<script>的链接或在地址栏中键入此URL并按Enter键完全相同。

但是,使用“返回”按钮会发生的事情是index.html中<script> $(document).ready(function () { AudioFileApp.initialize(); }); </script> 标记中的代码再次执行:

history already started

因此,我的视图无法正确创建(如果您只输入URL并按Enter键,则会出现这种情况)。我怀疑这个问题与这个turbolinks / Backbone / Rails问题有关,因为它解决了我遇到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(); }); } 错误的问题:documentation。但是,我仍然在重新初始化方面遇到问题,作为初学者,我完全难以接受。

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

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

populated? = City.inhabitants.any?

1 个答案:

答案 0 :(得分:0)