| Start |
|---|
| Binding to turbolink's concept of a page load With turbolinks, the traditional approach to using: won't work. While using turbolinks, the document.ready() syntax above, you'll have to bind to turbolink's visit event like so: |
| Disable turbolinks on specific links It is very easy to disable turbolinks on specific links. According to the official turbolinks documentation (opens new window): Turbolinks can be disabled on a per-link basis by annotating a link or any of its ancestors with data-turbolinks="false". Examples: |
| Understanding Application Visits Application visits are initiated by clicking a Turbolinks-enabled link, or programmatically by calling By default, the visit function uses the 'advance' action. More understandably, the default behavior for the visit function is to advance to the page indicated by the "location" parameter. Whenever a page is visited, turbolinks pushes a new entry onto the browser's history using However, if you want to visit a location without pushing any history onto the stack, you can use the 'replace' action on the visit function like so: This will replace the top of the history stack with the new page so that the total number of items on the stack remains unchanged. There is also a "restore" action that aids in restoration vists (opens new window), the visits that occur as a result of the user clicking the forward button or back button on their browser. Turbolinks handles these types of events internally and recommends that users don't manually tamper with the default behavior. |
| Cancelling visits before they begin Turbolinks provides an event listener that can be used to stop visits from occurring. Listen to the In the event handler, you can use: or to retrieve the location of the visit. The visit can then be cancelled by calling: NOTE:According to the official turbolinks docs (opens new window): Restoration visits cannot be canceled and do not fire turbolinks:before-visit. |
| Persisting elements across page loads Consider the following situation: Imagine that you are the developer of a social media website that allows users to be friends with other users and that employs turbolinks to make page loading faster. In the top right of every page on the site, there is a number indicating the total number of friends that a user currently has. Imagine you are using your site and that you have 3 friends. Whenever a new friend is added, you have some javascript that runs which updates the friend counter. Imagine that you just added a new friend and that your javascript ran properly and updated the friend count in the top right of the page to now render 4. Now, imagine that you click the browser's back button. When the page loads, you notice that the friend counter says 3 even though you have four friends. This is a relatively common problem and one that turbolinks has provided a solution for. The reason the problem occurs is because turbolinks automatically loads pages from the cache when a user clicks the back button. The cached page won't always be updated with the database. To solve this issue, imagine that you render the friend count inside a tag with an id of "friend-count": By adding the Designate permanent elements by giving them an HTML id and annotating them with data-turbolinks-permanent. Before each render, Turbolinks matches all permanent elements by id and transfers them from the original page to the new page, preserving their data and event listeners. RemarksAs a rails developer, you will likely interact minimally with turbolinks during your development. It is, however, an important library to be familiar with because it can be the cause of some hard-to-find bugs. Key takeaways:
For a more in-depth treatment of turbolinks, visit the official github repository (opens new window). Credit for much of this documentation goes to the folks who drafted the turbolinks documentation on the github repository. |
Read Article
https://develop.horje.com/learn/1434/reference