Skip to content

Commit

Permalink
fix(hashHistory): Fix the problem that can't build url right with onl…
Browse files Browse the repository at this point in the history
…y query in hash mode
  • Loading branch information
JOU-amjs committed Feb 24, 2018
1 parent edb18c3 commit 51ddbf5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
4 changes: 0 additions & 4 deletions src/core/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ export default function Module ( moduleElem, vmData = {} ) {
else {
const scopedCssObject = this.scopedCssObject;
appendScopedAttr ( moduleElem, scopedCssObject.selectors, scopedCssObject.identifier );

// 为带有href属性的vnode绑定点击事件
// 此函数只有在单页模式下才会被调用
// walkVDOM ( moduleElem, routingHandler );
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/core/ViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ function cloneNodeMaps ( originVal, newVal, newValBackup ) {
function initMethod ( methods, context ) {
foreach ( methods, ( method, key ) => {
context [ key ] = function ( ...args ) {
const nt = new NodeTransaction ().start ();
method.apply ( context, args );
const
nt = new NodeTransaction ().start (),
ret = method.apply ( context, args );

// 提交节点更新事物,更新所有已更改的vnode进行对比
nt.commit ();

return ret;
};
} );
}
Expand Down
1 change: 1 addition & 0 deletions src/router/history/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {

initHistory ( historyMode ) {
if ( !this.history ) {
this.mode = historyMode;

this.history =
( historyMode === HASH
Expand Down
18 changes: 11 additions & 7 deletions src/router/history/hashHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,31 @@ export default {
http://amaple.org/######
*/
buildURL ( path, mode ) {
const rquery = /\?.*?$/;
let host = window.location.host,
pathname = window.location.hash.replace ( rquery, "" ),
search = "";
path = path.replace ( /\s*http(?:s)?:\/\/(.+?\/|.+)/, ( match, rep ) => {
host = rep;
return "";
} )
.replace ( /\?.*?$/, match => {
.replace ( rquery, match => {
search = match;
return "";
} );

const pathname = ( window.location.hash || "#/" ).replace (
path.substr ( 0, 1 ) === "/" ? /#(.*)$/ : /\/([^\/]*)$/,
( match, rep ) => {
return match.replace ( rep, "" ) + path;
} );
if ( path !== "" ) {
pathname = ( pathname || "#/" ).replace (
path.substr ( 0, 1 ) === "/" ? /#(.*)$/ : /\/([^\/]*)$/,
( match, rep ) => {
return match.replace ( rep, "" ) + path;
} );
}

return {
host,
search,
pathname : pathname.substr ( 1 )
pathname : pathname.substr ( 0, 1 ) === "#" ? pathname.substr ( 1 ) : pathname
};
},

Expand Down
38 changes: 7 additions & 31 deletions src/router/routingHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { runtimeErr } from "../error";
import { foreach } from "../func/util";
import { serialize, attr } from "../func/node";
import { walkVDOM } from "../func/private";
import { amAttr } from "../var/const";
import { HASH } from "./history/historyMode";
import Router from "../router/Router";
import http from "../http/core";
import amHistory from "./history/core";
Expand Down Expand Up @@ -87,11 +89,11 @@ function getRoutingElem ( elem, rootElem, eventType ) {
URL doc:
http://amaple.org/######
*/
export default function routing ( e ) {
export default function routingHandler ( e ) {
const
eventType = e.type.toLowerCase (),
{ elem, path } = getRoutingElem ( e.target, this, eventType );
if ( path && !/#/.test ( path ) ) {
if ( path && !/http(?:s)?:\/\/|mailto:|#/i.test ( path ) ) {

const
method = eventType === "submit" ? ( attr ( elem, "method" ) || "POST" ).toUpperCase () : "GET",
Expand All @@ -113,34 +115,8 @@ export default function routing ( e ) {
}
}
}
}

/**
routingHandler ( vnode: Object )
Return Type:
void
Description:
为非组件vnode、组件的视图vnodes绑定路由跳转事件(click或submit)
URL doc:
http://amaple.org/######
*/
function routingHandler ( vnode ) {
if ( !vnode.isComponent ) {
if ( vnode.nodeType === 1 ) {
if ( vnode.attr ( amAttr.href ) ) {
vnode.bindEvent ( "click", routing );
}
else if ( vnode.attr ( amAttr.action ) && vnode.nodeName === "FORM" ) {
vnode.bindEvent ( "submit", routing );
}
}
}
else {
foreach ( vnode.templateNodes, childVNode => {
walkVDOM ( childVNode, routingHandler );
} );
else if ( /^#/.test ( path ) && amHistory.mode === HASH ) {
e.preventDefault ();
throw runtimeErr ( "redirect", "hash模式下不可使用形如'#...'的锚链接作为href的值" );
}
}

0 comments on commit 51ddbf5

Please sign in to comment.