From 0997999497617e358e0132058ceeb23d9ae34a73 Mon Sep 17 00:00:00 2001 From: Marc Grabanski Date: Sat, 7 May 2022 14:23:59 -0500 Subject: [PATCH] Add the ability to edit todos in another window --- js/store.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/js/store.js b/js/store.js index 394e522..178a7f6 100644 --- a/js/store.js +++ b/js/store.js @@ -2,7 +2,15 @@ export const TodoStore = class extends EventTarget { constructor(localStorageKey) { super(); this.localStorageKey = localStorageKey; - this.todos = JSON.parse(window.localStorage.getItem(localStorageKey) || '[]'); + this._readStorage(); + // handle if todos are edited in another window + window.addEventListener("storage", () => { + this._readStorage(); + this._save(); + }, false); + } + _readStorage () { + this.todos = JSON.parse(window.localStorage.getItem(this.localStorageKey) || '[]'); } _save() { window.localStorage.setItem(this.localStorageKey, JSON.stringify(this.todos));