scroll to top on route

This commit is contained in:
Leo Horie 2014-06-18 06:46:50 -04:00
parent 4aad8619c5
commit 81f3367be9
2 changed files with 5 additions and 3 deletions

View file

@ -360,7 +360,7 @@ Mithril = m = new function app(window) {
redirect(window.location[m.route.mode]) redirect(window.location[m.route.mode])
} }
} }
computePostRedrawHook = scrollToHash computePostRedrawHook = setScroll
window[listener]() window[listener]()
currentRoute = normalizeRoute(window.location[m.route.mode]) currentRoute = normalizeRoute(window.location[m.route.mode])
} }
@ -385,7 +385,7 @@ Mithril = m = new function app(window) {
if (window.history.pushState) { if (window.history.pushState) {
computePostRedrawHook = function() { computePostRedrawHook = function() {
window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute) window.history[shouldReplaceHistoryEntry ? "replaceState" : "pushState"](null, window.document.title, modes[m.route.mode] + currentRoute)
scrollToHash() setScroll()
} }
redirect(modes[m.route.mode] + currentRoute) redirect(modes[m.route.mode] + currentRoute)
} }
@ -425,8 +425,9 @@ Mithril = m = new function app(window) {
e.preventDefault() e.preventDefault()
m.route(e.currentTarget[m.route.mode].slice(modes[m.route.mode].length)) m.route(e.currentTarget[m.route.mode].slice(modes[m.route.mode].length))
} }
function scrollToHash() { function setScroll() {
if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash if (m.route.mode != "hash" && window.location.hash) window.location.hash = window.location.hash
else window.scrollTo(0, 0)
} }
function buildQueryString(object, prefix) { function buildQueryString(object, prefix) {
var str = [] var str = []

View file

@ -70,6 +70,7 @@ mock.window = new function() {
this.childNodes.splice(index, 1) this.childNodes.splice(index, 1)
child.parentNode = null child.parentNode = null
} }
window.scrollTo = function() {}
window.performance = new function () { window.performance = new function () {
var timestamp = 50 var timestamp = 50
this.$elapse = function(amount) {timestamp += amount} this.$elapse = function(amount) {timestamp += amount}