rails/turbolinks — pull request #139 by reednj77 (2012-12-10 23:22:03+00:00)
Title: Refactor to remove duplication

**Changes**

* Merges `recallScrollPosition()` and `resetScrollPosition()` into:

```coffeescript
resetScrollPosition = (page) ->
window.scrollTo page?.positionX ? 0, page?.positionY ? 0
```

* Moves the `resetScrollPosition()` method calls from the two fetch methods into the `changePage()` method.

* Pulls the initialization check out of `rememberInitialPage()` and into `cacheCurrentPage()`, enhancing readability and preventing unnecessary method calls.

```coffeescript
initialized ||= rememberInitialPage()
```

* Removes the `extractTitleAndBody()` method. Unless there's an edge case I'm unaware of, `doc.querySelector('title').textContent` can be replaced with `doc.title`. This means that a new page and a cached page both have `title` and `body` attributes, so I've simplified the parameters of the `changePage()` method.

```coffeescript
changePage = (page, cached = false) ->
document.title = page.title if page.title?
document.documentElement.replaceChild page.body, document.body
executeScriptTags() unless cached
rememberCurrentState()
resetScrollPosition page
triggerEvent 'page:change'

# In fetchReplacement()
changePage doc

# In fetchHistory()
changePage page, true
```