From 7ab20b0917ff614b8457289e3dab3a009bc34190 Mon Sep 17 00:00:00 2001 From: yang qianjun Date: Fri, 23 Jan 2015 19:05:17 +0800 Subject: [PATCH] fix url & params bug --- mmHistory.js | 25 ++++++++++++++----------- mmRouter.js | 10 ++++++---- mmState.js | 13 +++++++++---- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/mmHistory.js b/mmHistory.js index bee6234..cb8f4dd 100644 --- a/mmHistory.js +++ b/mmHistory.js @@ -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 @@ -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 } } } @@ -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) } } }) diff --git a/mmRouter.js b/mmRouter.js index 3197b10..d46b418 100644 --- a/mmRouter.js +++ b/mmRouter.js @@ -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' diff --git a/mmState.js b/mmState.js index 5e44470..2b125f5 100644 --- a/mmState.js +++ b/mmState.js @@ -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") } } } @@ -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();