Skip to content

Commit

Permalink
Track assets named 'data-turbolinks-track'
Browse files Browse the repository at this point in the history
This commit changes turbolinks' behavior.
Track "data-turbolinks-track" assets in current page,
and trigger document.reload() ONLY when clicked link page's
"data-turbolinks-track"(s) differ from those of current page.
Otherwise browser always uses Turbolinks#visit.
  • Loading branch information
yasuoza committed Dec 4, 2012
1 parent 5f156b3 commit 68720db
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/assets/javascripts/turbolinks.js.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
initialized = false
currentState = null
referer = document.location.href
assets = []
trackingAssets = []
pageCache = {}
createDocument = null

Expand Down Expand Up @@ -99,8 +99,8 @@ rememberCurrentUrl = ->
rememberCurrentState = ->
currentState = window.history.state

rememberCurrentAssets = ->
assets = extractAssets document
rememberCurrentTrackingAssets = ->
trackingAssets = extractTrackAssets document

rememberInitialPage = ->
unless initialized
Expand All @@ -121,13 +121,13 @@ triggerEvent = (name) ->
event.initEvent name, true, true
document.dispatchEvent event

extractTrackAssets = (doc) ->
(node.src || node.href) for node in doc.head.childNodes when node.getAttribute?('data-turbolinks-track')?

extractAssets = (doc) ->
(node.src || node.href) for node in doc.head.childNodes when node.src or node.href

assetsChanged = (doc)->
extractedAssets = extractAssets doc
extractedAssets.length isnt assets.length or intersection(extractedAssets, assets).length != assets.length
extractedTrackAssets = extractTrackAssets doc
extractedTrackAssets.length isnt trackingAssets.length or intersection(extractedTrackAssets, trackingAssets).length != trackingAssets.length

intersection = (a, b) ->
[a, b] = [b, a] if a.length > b.length
Expand Down Expand Up @@ -205,7 +205,7 @@ browserSupportsPushState =
window.history and window.history.pushState and window.history.replaceState and window.history.state != undefined

if browserSupportsPushState
rememberCurrentAssets()
rememberCurrentTrackingAssets()
document.addEventListener 'click', installClickHandlerLast, true

window.addEventListener 'popstate', (event) ->
Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<li><a href="/other.html"><span>Wrapped link</span></a></li>
<li><a href="http://www.google.com/">Cross origin</a></li>
<li><a href="/other.html" onclick="if(!confirm('follow link?')) { return false}">Confirm Fire Order</a></li>
<li><a href="/reload.html"><span>Asset Change</span></a></li>
<li><a href="/reload.html"><span>New assets track </span></a></li>
<li><a href="/dummy.gif?12345">Query Param Image Link</a></li>
<li><a href="#">Hash link</a></li>
<li><a href="/reload.html#foo">Asset Change with hash link</a></li>
<li><a href="/reload.html#foo">New assets track with hash link</a></li>
</ul>

<div style="background:#ccc;height:5000px;width:200px;">
Expand Down
2 changes: 1 addition & 1 deletion test/reload.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Home</title>
<script type="text/javascript" src="/js/turbolinks.js?1"></script>
<script type="text/javascript" src="/js/turbolinks.js?1" data-turbolinks-track='true'></script>
<script type="text/javascript">
document.addEventListener("page:change", function() {
console.log("page changed");
Expand Down

0 comments on commit 68720db

Please sign in to comment.