Skip to content

Commit 1edde25

Browse files
committed
add lastState property
1 parent c6cd4db commit 1edde25

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"navigo": "^7.1.1",
3131
"spiel-render": "^1.0.1",
3232
"typescript": "^2.7.2",
33-
"ultradom": "git+ssh://[email protected]/jorgebucaran/ultradom.git#options"
33+
"ultradom": "^2.2.0"
3434
},
3535
"devDependencies": {
3636
"@types/chai": "^4.1.2",

src/router.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export class Router {
5353
* @param absolute True allow to pass absolute path
5454
* @example router.go("http://localhost/example", true)
5555
*/
56-
public go(path: string, absolute?: boolean) {
56+
public go(path: string, state?: object | null, absolute?: boolean) {
57+
if (state) {
58+
path = `${(path.indexOf("?") !== -1) ?
59+
`${path}&` :
60+
`${path}?`}state=${encodeURIComponent(JSON.stringify(state))}`;
61+
}
5762
this.router.navigate(path, absolute);
5863
}
5964

@@ -115,13 +120,38 @@ export class Router {
115120
private setPatch(route: IRouters, params: object, query: string) {
116121
const page = route.page;
117122
const state: State = {};
123+
if (query) {
124+
state.lastState = this.checkState(query);
125+
}
118126
Object.assign(state, page.state);
119127
state.params = params;
120-
state.query = query;
128+
state.query = this.checkQuery(query);
121129
state.defaultProps = route.defaultProps || this.defaultProps;
122130
render(page.view, state, this.element);
123131
}
124132

133+
private checkQuery(query: string) {
134+
if (query && query.indexOf("&state=") !== -1) {
135+
query = query.substr(0, query.indexOf("&state="));
136+
}
137+
138+
return query;
139+
}
140+
141+
private checkState(query: string) {
142+
let state: string | object = "";
143+
const index = query.indexOf("state=");
144+
145+
if (index !== -1 ) {
146+
state = query.substr(index);
147+
state = decodeURIComponent(state);
148+
state = state.replace("state=", "");
149+
state = JSON.parse(state);
150+
}
151+
152+
return state;
153+
}
154+
125155
private checkNotFound() {
126156
this.router.notFound(() => {
127157
if (this.configRouters.notFound && this.configRouters.notFoundPath &&

test/Router/Router.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("Router", () => {
120120
setTimeout(() => {
121121
const title = document.getElementsByTagName("h1")[0];
122122
const defaultProps = document.getElementsByTagName("h2")[0];
123-
expect(title.textContent).has.to.be.equal("Really test 4 state=good");
123+
expect(title.textContent).has.to.be.equal("Really test 4 query=really good");
124124
expect(defaultProps.textContent).has.to.be.equal("default");
125125
srouter.go("/home");
126126
done();
@@ -241,7 +241,7 @@ describe("Router", () => {
241241
});
242242
srouter.router.resume();
243243
srouter.resolve();
244-
srouter.go("http://localhost:9876/", true);
244+
srouter.go("http://localhost:9876/", null, true);
245245
setTimeout(() => {
246246
const element = document.getElementsByTagName("h1")[0];
247247
expect(element.textContent).has.to.be.equal("Hello world");

test/Router/mocks/TestPage1.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class TestPage1 {
1717
>go to child</button>
1818
<button id="grandchild"
1919
onclick = {() => {
20-
srouter.go("/home/child/2/child2/test?state=good");
20+
srouter.go("/home/child/2/child2/test?query=really", {text: "good"});
2121
}}
2222
>go to child 2</button>
2323
<button id="brother"

test/Router/mocks/TestPage3.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class TestPage3 {
88
public view(state: State): JSXElements {
99
return (
1010
<div>
11-
<h1>{state.title} {state.params.word} {state.params.number} {state.query}</h1>
11+
<h1>{state.title} {state.params.word} {state.params.number} {state.query} {state.lastState.text}</h1>
1212
<h2>{state.defaultProps}</h2>
1313
<button
1414
onclick = {() => {

0 commit comments

Comments
 (0)