codebrew / backbone-rails

Easily use backbone.js with rails 3.1
MIT License
1.62k stars 255 forks source link

In combination with turbolinks getting error #134

Open pbrazdil opened 11 years ago

pbrazdil commented 11 years ago

Backbone-rails isn't compatible with Turbolinks gem, which it will has to be since turbolinks gem going to be in Rails 4.0

When I load page and then click on some link which will be processed by turbolinks, getting error below

Uncaught Error: Backbone.history has already been started

hyperrjas commented 11 years ago

I have the same problem +1

hyperrjas commented 11 years ago

I have fixed the problem. I don't know if this is correct:

initialize: ->
  new MyApp.Routers.Posts
  Backbone.history.start() unless Backbone.History.started
  $(document).on "page:change", ->
    Backbone.history.stop()
    Backbone.history.start()

Please correct me if I'm wrong. thanks

carlosmoralesla commented 8 years ago

I tried your code but seems not to be running. Are you sure that "initialize: ->" code is correct?

initialize: -> new MyApp.Routers.Posts Backbone.history.start() unless Backbone.History.started $(document).on "page:change", -> Backbone.history.stop() Backbone.history.start()

pbrazdil commented 8 years ago

It's 3 years old čt 25. 8. 2016 v 6:50 odesílatel Carlos Morales notifications@github.com napsal:

I tried your code but seems not to be running. Are you sure that "initialize: ->" code is correct?

initialize: -> new MyApp.Routers.Posts Backbone.history.start() unless Backbone.History.started $(document).on "page:change", -> Backbone.history.stop() Backbone.history.start()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/codebrew/backbone-rails/issues/134#issuecomment-242280407, or mute the thread https://github.com/notifications/unsubscribe-auth/ABYDLfvcGnMZM4szmsTAwzIJQdrCduSoks5qjR8PgaJpZM4AUBDl .

DanTaiko commented 7 years ago

I had the same problem. The solution of @hyperrjas should be workable, unless turbolink is set.

With 'turbolink' I got the following possible solution that works for me (enviroment: Rails v5.1.1, jQuery v3.2.1, Backbone 1.2.0):

initialize: ->
    new MyApp.Routers.Posts
    Backbone.history.start() unless Backbone.History.started
    $(document).on "turbolinks:click", ->
      Backbone.history.stop() 

Or even more simple one, depending of your need:

initialize: ->
    new MyApp.Routers.Posts
    Backbone.history.start() unless Backbone.History.started

I don't think it's a Backbone-rails bug, it lies in understanding of the Backbone.routing and Turbolink events.

Hope it will be helpfull to someone. ;-)