From 628e99ba2d0c36deaee0d28837fcf2cc0947d1a3 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 30 Aug 2021 11:08:58 -0700 Subject: [PATCH] Cleanup view switching code a bit --- src/RootViewModel.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/RootViewModel.js b/src/RootViewModel.js index be416df..2cc1070 100644 --- a/src/RootViewModel.js +++ b/src/RootViewModel.js @@ -35,10 +35,13 @@ export class RootViewModel extends ViewModel { }); } - _updateChildVMs(oldLink) { - if (!oldLink || !oldLink.equals(this.link)) { + _updateChildVMs(newLink, oldLink) { + this.link = newLink; + if (!newLink) { + this.openLinkViewModel = null; + } else if (!oldLink || !oldLink.equals(newLink)) { this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({ - link: this.link, + link: newLink, clients: createClients(), })); } @@ -51,28 +54,27 @@ export class RootViewModel extends ViewModel { } updateHash(hash) { - // All view models except openLink are re-created anyway. - // Might as well clear them to avoid having to - // manually reset (n-1)/n view models in every case. + // All view models except openLink are re-created anyway. Might as well + // clear them to avoid having to manually reset (n-1)/n view models in every case. + // That just doesn't scale well when we add new views. const oldLink = this.link; - this.link = null; this.showDisclaimer = false; this.loadServerPolicyViewModel = null; this.createLinkViewModel = null; + let newLink; if (hash.startsWith("#/policy/")) { const server = hash.substr(9); - this.openLinkViewModel = null; + this._updateChildVMs(null, oldLink); this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server})); this.loadServerPolicyViewModel.load(); } else if (hash.startsWith("#/disclaimer/")) { - this.openLinkViewModel = null; + this._updateChildVMs(null, oldLink); this.showDisclaimer = true; } else if (hash === "" || hash === "#" || hash === "#/") { - this.openLinkViewModel = null; + this._updateChildVMs(null, oldLink); this.createLinkViewModel = new CreateLinkViewModel(this.childOptions()); - } else if (this.link = Link.parse(hash)) { - this.link = Link.parse(hash); - this._updateChildVMs(oldLink); + } else if (newLink = Link.parse(hash)) { + this._updateChildVMs(newLink, oldLink); } this.emitChange(); }