LoginSignup
18
18

More than 5 years have passed since last update.

TurbolinksとBackbone.jsを共存させるための二行

Posted at

小ネタ

Backbone.jsのディスパッチにRouterを使うことは良くある。
ページが単純に読み込まれるなら、ページ読み込み時にBackbone.history.start({pushState: true})すれば良いのだが、Turbolinksだとリンク辿っても既にstart済みのまま。
かといってBackboneのRouterはpopstateイベントに反応するので、Turblinksでページ遷移してもURL変わるだけでRouterが反応しない。

なので、イベントを足しておく。

# hack for Turbolinks
$(document).on "page:load", ->
  Backbone.history.loadUrl()

Turbolinksの遷移が完了した際には、とりあえずloadUrl()読んでURLを確認する。もしかしたらcheckUrl()の方が良いかもしれない。

pushStateが使えないブラウザとかは知らんw

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18