Skip to content

Commit

Permalink
Add the ability to edit todos in another window
Browse files Browse the repository at this point in the history
  • Loading branch information
1Marc committed May 7, 2022
1 parent 854dd40 commit 0997999
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion js/store.js
Expand Up @@ -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));
Expand Down

0 comments on commit 0997999

Please sign in to comment.