Skip to content

Instantly share code, notes, and snippets.

@jayphelps
Last active April 23, 2016 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayphelps/0bfc9c5b080deefa51c2 to your computer and use it in GitHub Desktop.
Save jayphelps/0bfc9c5b080deefa51c2 to your computer and use it in GitHub Desktop.
Ember router history Location class that keeps scroll position between transitions
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