Last active
April 23, 2016 04:45
-
-
Save jayphelps/0bfc9c5b080deefa51c2 to your computer and use it in GitHub Desktop.
Ember router history Location class that keeps scroll position between transitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var get = Ember.get; | |
var HistoryScrollLocation = Ember.HistoryLocation.extend({ | |
document: document, | |
window: window, | |
_storeScrollPos: function () { | |
var state = history.state, | |
window = get(this, 'window'), | |
doc = get(this, 'document.documentElement'); | |
state.scrollX = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0); | |
state.scrollY = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); | |
get(this, 'history').replaceState(state); | |
}, | |
pushState: stateHook, | |
replaceState: stateHook | |
}); | |
function stateHook(path) { | |
this._storeScrollPos(); | |
return this._super(path); | |
} | |
var AutoWithScrollLocation = Ember.AutoLocation.extend({ | |
// This is techncially a private property, so watch out | |
// if you use this! It could change between versions! | |
_HistoryLocation: HistoryScrollLocation | |
}); | |
Ember.Application.initializer({ | |
name: 'auto-with-scroll', | |
initialize: function (container, application) { | |
container.register('location:auto-with-scroll', AutoWithScrollLocation); | |
} | |
}); | |
// Later in your app | |
App.Router.reopen({ | |
location: 'auto-with-scroll' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment