core router: add debounceAsync(), normalize onhashchange and onpopstate timing. Fix ##1269
This commit is contained in:
parent
879b9d6883
commit
0539fc651d
1 changed files with 27 additions and 17 deletions
|
|
@ -16,6 +16,18 @@ module.exports = function($window) {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var asyncId
|
||||||
|
function debounceAsync(f) {
|
||||||
|
return function(){
|
||||||
|
if (asyncId != null) return
|
||||||
|
asyncId = callAsync(function(){
|
||||||
|
asyncId = null
|
||||||
|
f()
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function parsePath(path, queryData, hashData) {
|
function parsePath(path, queryData, hashData) {
|
||||||
var queryIndex = path.indexOf("?")
|
var queryIndex = path.indexOf("?")
|
||||||
var hashIndex = path.indexOf("#")
|
var hashIndex = path.indexOf("#")
|
||||||
|
|
@ -67,7 +79,7 @@ module.exports = function($window) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function defineRoutes(routes, resolve, reject) {
|
function defineRoutes(routes, resolve, reject) {
|
||||||
if (supportsPushState) $window.onpopstate = resolveRoute
|
if (supportsPushState) $window.onpopstate = debounceAsync(resolveRoute)
|
||||||
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
else if (prefix.charAt(0) === "#") $window.onhashchange = resolveRoute
|
||||||
resolveRoute()
|
resolveRoute()
|
||||||
|
|
||||||
|
|
@ -76,7 +88,6 @@ module.exports = function($window) {
|
||||||
var params = {}
|
var params = {}
|
||||||
var pathname = parsePath(path, params, params)
|
var pathname = parsePath(path, params, params)
|
||||||
|
|
||||||
callAsync(function() {
|
|
||||||
for (var route in routes) {
|
for (var route in routes) {
|
||||||
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
var matcher = new RegExp("^" + route.replace(/:[^\/]+?\.{3}/g, "(.*?)").replace(/:[^\/]+/g, "([^\\/]+)") + "\/?$")
|
||||||
|
|
||||||
|
|
@ -94,7 +105,6 @@ module.exports = function($window) {
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(path, params)
|
reject(path, params)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return resolveRoute
|
return resolveRoute
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue