イベント
Turboは、ナビゲーション・ライフサイクルを追跡しページの読み込みに返答できるイベントを発行します。
特記がない限り、Turboはdocument.documentElementオブジェクト(つまり、要素)でイベントを発生させます。これにより、ページの読み込みやナビゲーションのさまざまな段階でイベントをキャッチして処理することが可能になります。
(jQuery を利用する際は、イベント上のデータが $event.originalEvent.detail としてアクセスできなければならないことに注意してください)
- 
turbo:clickは Turbo が有効になったリンクがクリックされた際に発火します。クリックされた要素がイベントのターゲットとなります。リンク先はevent.detail.urlで取得できます。このイベントをキャンセルすると、クリックはブラウザに普通のナビゲーションとしてそのまま渡されます。
- 
turbo:before-visitはあるロケーションへアクセスする前に発火します(ブラウザの履歴からのナビゲーションはのぞきます)。event.detail.urlでリクエストされたロケーションが取得できます。このイベントをキャンセルすると、ナビゲーションが中止されます。
- 
turbo:visitはナビゲーションが開始した直後に発火します。event.detail.urlでリクエストされたロケーションが、event.detail.actionでアクションが取得できます。
- 
turbo:submit-startはフォームがサブミットされる間に発火します。FormSubmissionオブジェクトにはevent.detail.formSubmissionでアクセスできます。サブミッションを棄却する(例えばバリデーションに失敗した後など)には、event.detail.formSubmission.stop()が使えます(jQueryを使っている場合はevent.originalEvent.detail.formSubmission.stop()を使いましょう)。
- 
turbo:before-fetch-requestは、Turbo がページの取得のためにネットワークリクエストを発行する前に発火します。リクエスト先はevent.detail.urlで取得できます。また、オプションオブジェクトはevent.detail.fetchOptionsで取得できます。このイベントが発火されるのは、そのイベントを引き起こしたそれぞれの要素(turboフレームあるいはフォーム要素)です。要素は、event.targetプロパティでアクセスできます。リクエストはevent.detail.resumeによってキャンセル、あるいは継続されます(詳細は リクエストの停止参照)。
- 
turbo:before-fetch-responseはネットワークリクエストの完了前に発火します。フェッチのオプションオブジェクトはevent.detailで取得できます。このイベントが発火されるのは、そのイベントを引き起こしたそれぞれの要素(turboフレームあるいはフォーム要素)です。その要素には、event.targetプロパティでアクセスできます。
- 
turbo:submit-endはフォームのサブミッションによって開始したネットワークリクエストが完了した後に発火します。FormSubmissionオブジェクトはevent.detail.formSubmissionでアクセスできます。また、event.detail内に含まれるFormSubmissionResultプロパティも同様にアクセスできます。
- 
turbo:before-cacheは Turbo が現在のページをキャッシュする前に発火します。
- 
turbo:before-renderはページの描画前に発火します。新しい<body>要素にはevent.detail.newBodyでアクセスできます。描画はevent.detail.resumeでキャンセルまたは継続できます(詳細は 描画の一時停止)。 Turbo ドライブがレスポンスを描画する方法は、event.detail.render関数で上書きしてカスタマイズできます (詳細は 描画のカスタマイズ)。
- 
turbo:before-stream-renderは Turbo ストリームがページの更新を描画する前に発火します。新しい<turbo-stream>には、event.detail.newStreamでアクセスできます。要素の振る舞いをカスタマイズするには、event.detail.render関数を上書きしてください(詳細はアクションのカスタマイズ)。
- 
turbo:renderは Turbo がページを描画した後に発火します。このイベントは、アプリケーションがキャッシュされたロケーションにアクセスする間、二度発火します。一度目はキャッシュされたバージョンが描画された後、二度目は新しいバージョンが描画された後です。
- 
turbo:loadは初めてのページがロードされた後に発火し、それからTurboがvisitするごとに発火します。event.detail.timingオブジェクトを使って、visitのタイムングのメトリクスを取得できます。
- 
turbo:before-frame-renderは<turbo-frame>要素を描画する前に発火します。新しい<turbo-frame>要素はevent.detail.newFrameで取得できます。描画はevent.detail.resumeによってキャンセルまたは継続できます。(詳細は 描画の一時停止)。Turbo ドライブがレスポンスをどのように描画するかは、event.detail.render関数を上書きしてカスタマイズできます。 (詳細は 描画のカスタマイズ)。
- 
turbo:frame-renderは<turbo-frame>要素が描画された直後に発火します。特定の<turbo-frame>要素がイベントの対象になります。FetchResponseオブジェクトは、event.detail.fetchResponseプロパティで取得できます。
- 
turbo:frame-loadは<turbo-frame>要素がナビゲートされ、ロードが終了した時点で発火します(turbo:frame-renderの後です)。特定の<turbo-frame>要素がイベントの対象になります。
- 
turbo:frame-missingは、<turbo-frame>要素へのリクエストのレスポンスが、マッチする<turbo-frame>要素を含んでいなかったときに発火します。デフォルトでは、Turbo はフレームに対してメッセージの形で情報を提供し、例外を投げます。この挙動をキャンセルするにはこの取り扱いを上書きします。レスポンスインスタンスにはevent.detail.responseでアクセスでき、event.detail.visit(...)を呼ぶことでvisitできます。
- 
turbo:fetch-request-errorはフォームかフレームが取ってこようとしたリクエストが、ネットワークエラーで失敗したときに発火します。このイベントはイベントを起動した各々の要素(turboフレームかフォーム要素)上に発火し、event.targetプロパティでアクセスできます。このイベントはキャンセルできます。
原文
Events
Turbo emits events that allow you to track the navigation lifecycle and respond to page loading. Except where noted, Turbo fires events on the document.documentElement object (i.e., the <html> element).
(Note that when using jQuery, the data on the event must be accessed as $event.originalEvent.detail.)
- turbo:clickfires when you click a Turbo-enabled link. The clicked element is the event target. Access the requested location with- event.detail.url. Cancel this event to let the click fall through to the browser as normal navigation.
- turbo:before-visitfires before visiting a location, except when navigating by history. Access the requested location with- event.detail.url. Cancel this event to prevent navigation.
- turbo:visitfires immediately after a visit starts. Access the requested location with- event.detail.urland action with- event.detail.action.
- turbo:submit-startfires during a form submission. Access the- FormSubmissionobject with- event.detail.formSubmission. Abort form submission (e.g. after validation failure) with- event.detail.formSubmission.stop(). (use- event.originalEvent.detail.formSubmission.stop()if you’re using jQuery).
- turbo:before-fetch-requestfires before Turbo issues a network request to fetch the page. Access the requested location with- event.detail.urland the fetch options object with- event.detail.fetchOptions. This event fires on the respective element (turbo-frame or form element) which triggers it and can be accessed with- event.targetproperty. Request can be canceled and continued with- event.detail.resume(see Pausing Requests).
- turbo:before-fetch-responsefires after the network request completes. Access the fetch options object with- event.detail. This event fires on the respective element (turbo-frame or form element) which triggers it and can be accessed with- event.targetproperty.
- turbo:submit-endfires after the form submission-initiated network request completes. Access the- FormSubmissionobject with- event.detail.formSubmissionalong with- FormSubmissionResultproperties included within- event.detail.
- turbo:before-cachefires before Turbo saves the current page to cache.
- turbo:before-renderfires before rendering the page. Access the new- <body>element with- event.detail.newBody. Rendering can be canceled and continued with- event.detail.resume(see Pausing Rendering). Customize how Turbo Drive renders the response by overriding the- event.detail.renderfunction (see Custom Rendering).
- turbo:before-stream-renderfires before rendering a Turbo Stream page update. Access the new- <turbo-stream>element with- event.detail.newStream. Customize the element’s behavior by overriding the- event.detail.renderfunction (see Custom Actions).
- turbo:renderfires after Turbo renders the page. This event fires twice during an application visit to a cached location: once after rendering the cached version, and again after rendering the fresh version.
- turbo:loadfires once after the initial page load, and again after every Turbo visit. Access visit timing metrics with the- event.detail.timingobject.
- turbo:before-frame-renderfires before rendering the- <turbo-frame>element. Access the new- <turbo-frame>element with- event.detail.newFrame. Rendering can be canceled and continued with- event.detail.resume(see Pausing Rendering). Customize how Turbo Drive renders the response by overriding the- event.detail.renderfunction (see Custom Rendering).
- turbo:frame-renderfires right after a- <turbo-frame>element renders its view. The specific- <turbo-frame>element is the event target. Access the- FetchResponseobject with- event.detail.fetchResponseproperty.
- turbo:frame-loadfires when a- <turbo-frame>element is navigated and finishes loading (fires after- turbo:frame-render). The specific- <turbo-frame>element is the event target.
- turbo:frame-missingfires when the response to a- <turbo-frame>element request does not contain a matching- <turbo-frame>element. By default, Turbo writes an informational message into the frame and throws an exception. Cancel this event to override this handling. You can access the Response instance with- event.detail.response, and perform a visit by calling- event.detail.visit(...).
- turbo:fetch-request-errorfires when a form or frame fetch request fails due to network errors. This event fires on the respective element (turbo-frame or form element) which triggers it and can be accessed with- event.targetproperty. This event can be canceled.