From 371fc19a0608a840e2366029a8aa1c6233d57fff Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Wed, 21 Mar 2018 18:38:09 +0100 Subject: [PATCH] fix(router): reusing checks params --- core/src/components/nav/nav.tsx | 21 +++++++------ core/src/components/nav/view-controller.ts | 30 +++++++++++++++++++ .../components/router/test/basic/index.html | 17 +++++++---- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/core/src/components/nav/nav.tsx b/core/src/components/nav/nav.tsx index 99a47ed91c..6f84ad6031 100644 --- a/core/src/components/nav/nav.tsx +++ b/core/src/components/nav/nav.tsx @@ -200,10 +200,12 @@ export class NavControllerBase implements NavOutlet { @Method() setRouteId(id: string, params: any = {}, direction: number): Promise { const active = this.getActive(); - if (active && active.component === id) { + + if (active && active.matches(id, params)) { return Promise.resolve({changed: false, element: active.element}); } - const viewController = this._views.find(v => v.component === id) || id; + + const viewController = this._views.find(v => v.matches(id, params)); let resolve: (result: RouteWrite) => void; const promise = new Promise((r) => resolve = r); @@ -220,17 +222,14 @@ export class NavControllerBase implements NavOutlet { return p; } }; - - if (direction === 1) { - this.push(viewController, params, commonOpts); + if (viewController) { + this.popTo(viewController, {...commonOpts, direction: NavDirection.back}); + } else if (direction === 1) { + this.push(id, params, commonOpts); } else if (direction === -1) { - this.setRoot(id, params, { - ...commonOpts, - direction: NavDirection.back, - animate: true - }); + this.setRoot(id, params, {...commonOpts, direction: NavDirection.back, animate: true}); } else { - this.setRoot(viewController, params, commonOpts); + this.setRoot(id, params, commonOpts); } return promise; } diff --git a/core/src/components/nav/view-controller.ts b/core/src/components/nav/view-controller.ts index 807e76f893..cae4304dcf 100644 --- a/core/src/components/nav/view-controller.ts +++ b/core/src/components/nav/view-controller.ts @@ -75,6 +75,36 @@ export class ViewController { this._leavingOpts = opts; } + matches(id: string, params: any): boolean { + if (this.component !== id) { + return false; + } + const currentParams = this.data; + const null1 = (currentParams == null); + const null2 = (params == null); + if (null1 !== null2) { + return false; + } + if (null1 && null2) { + return true; + } + + const keysA = Object.keys(currentParams); + const keysB = Object.keys(params); + if (keysA.length !== keysB.length) { + return false; + } + + // Test for A's keys different from B. + for (let i = 0; i < keysA.length; i++) { + const key = keysA[i]; + if (currentParams[key] !== params[key]) { + return false; + } + } + return true; + } + /** * @hidden * DOM WRITE diff --git a/core/src/components/router/test/basic/index.html b/core/src/components/router/test/basic/index.html index 1210cebb8a..caba2d7f37 100644 --- a/core/src/components/router/test/basic/index.html +++ b/core/src/components/router/test/basic/index.html @@ -17,7 +17,10 @@ page one - Ir a la page 2 +

Go to page 2

+

Go to page 3 (hola)

+

Go to page 3 (something)

+
`; } } @@ -31,7 +34,8 @@ - page two +

Go to page 3 (hola)

+

Go to page 3 (hello)

`; } } @@ -41,11 +45,14 @@ this.innerHTML = ` - Page 3 + Page 3 ${this.prop1} - page tres +

Go to page 3 (hola)

+

Go to page 3 (hello)

+

Go to page 2

+

Go to page 1

`; } } @@ -111,7 +118,7 @@ - +