Skip to content

Commit

Permalink
Merge pull request #38 from gogoyqj/master
Browse files Browse the repository at this point in the history
fix url & params bug
  • Loading branch information
RubyLouvre committed Jan 23, 2015
2 parents 277d389 + 7ab20b0 commit a60ecdd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
25 changes: 14 additions & 11 deletions mmHistory.js
Expand Up @@ -149,9 +149,10 @@ define(["avalon"], function(avalon) {
}
if (hash !== void 0) {
that.fragment = hash
if(!avalon.router.locked) that.fireRouteChange(hash)
// 状态改变,同步url的时候,不能再去触发状态变化
if(!avalon.history.locked) that.fireRouteChange(hash)
avalon.history.locked = false
}
avalon.router.locked =false
}

//thanks https://github.com/browserstate/history.js/blob/master/scripts/uncompressed/history.html4.js#L272
Expand Down Expand Up @@ -192,22 +193,24 @@ define(["avalon"], function(avalon) {
clearInterval(this.checkUrl)
History.started = false
},
updateLocation: function(hash) {
updateLocation: function(hash, doNotNotifyUrlChecker) {
if (this.monitorMode === "popstate") {
var path = this.rootpath + hash
// 防止同一個url觸發
// 防止多次觸發
if(this.location.pathname != path) {
history.pushState({path: path}, document.title, path)
this._fireLocationChange()
if(!doNotNotifyUrlChecker) this._fireLocationChange()
} else {
avalon.router.locked = false
avalon.history.locked = false
}
} else {
if(this.prefix + hash != this.location.hash) {
this.location.hash = this.prefix + hash
} else {
avalon.router.locked = false
if(doNotNotifyUrlChecker) {
avalon.history.locked = true
}
if(this.location.hash == this.prefix + hash) {
avalon.history.locked = false
}
this.location.hash = this.prefix + hash
}
}
}
Expand Down Expand Up @@ -239,7 +242,7 @@ define(["avalon"], function(avalon) {
var hash = href.replace(prefix, "").trim()
if (href.indexOf(prefix) === 0 && hash !== "") {
event.preventDefault()
avalon.history.updateLocation(hash)
avalon.router && avalon.router.navigate(hash)
}
}
})
Expand Down
10 changes: 6 additions & 4 deletions mmRouter.js
Expand Up @@ -121,12 +121,14 @@ define(["mmHistory"], function() {
setLastPath: function(path) {
setCookie("msLastPath", path)
},
navigate: function(hash) {
var parsed = parseQuery(hash)
// doNotNotifyUrlChecker,当这个参数为true的时候,是不能触发url检测监听的
navigate: function(hash, doNotNotifyUrlChecker) {
var parsed = parseQuery((hash.charAt(0) !== "/" ? "/" : "") + hash)
if(hash.charAt(0) === "/")
hash = hash.slice(1)// 修正出现多扛的情况 fix http://localhost:8383/mmRouter/index.html#!//
avalon.history.updateLocation(hash)
this.route("get", parsed.path, parsed.query)
// 只是写历史而已
avalon.history && avalon.history.updateLocation(hash, "doNotNotifyUrlChecker")
if(!doNotNotifyUrlChecker) this.route("get", parsed.path, parsed.query)
},
/* *
`'/hello/'` - 匹配'/hello/'或'/hello'
Expand Down
13 changes: 9 additions & 4 deletions mmState.js
Expand Up @@ -52,15 +52,14 @@ define("mmState", ["mmPromise", "mmRouter"], function() {
return to.params [el.name] || ""
})
mmState.transitionTo(from, to, args)
if(avalon.history && params && from != to) {
if(avalon.history && params) {
// 更新url
avalon.router.locked = true // 关闭历史监听,防止触发两次
var query = params.query ? queryToString(params.query) : "",
hash = to.url.replace(/\{[^\/\}]+\}/g, function(mat) {
var key = mat.replace(/[\{\}]/g, '')
return params[key] || ''
}).replace(/^\//g, '') + query
avalon.history.updateLocation(hash)
avalon.router.navigate(hash, "doNotNotifyUrlChecker")
}
}
}
Expand All @@ -72,18 +71,24 @@ define("mmState", ["mmPromise", "mmRouter"], function() {
mmState.prevState = fromState
mmState.currentState = toState
var states = []
var t = toState
var t = toState, tmp
if (!fromState) {
while (t) {
tmp = t
states.push(t)
t = t.parentState
// 共享params,解决父级状态获取不到参数
if(t && !t.params) t.params = tmp.params
}
} else if (fromState === toState) {
states.push(t)
} else {
while (t && t !== fromState) {
tmp = t
states.push(t)
t = t.parentState
// 共享params,解决父级状态获取不到参数
if(t && !t.params) t.params = tmp.params
}
}
states.reverse();
Expand Down

0 comments on commit a60ecdd

Please sign in to comment.