Skip to content

Commit

Permalink
feat: persistence avec localStorage et JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
bfritscher committed May 16, 2024
1 parent de67750 commit fb65a44
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ function find(func) {
}
}
*/
const LOCAL_STORAGE_KEY = "myapp-donations";
export default {
components: {
DonationItem,
DonationAdd
},
data() {
let donations = localStorage.getItem(LOCAL_STORAGE_KEY);
if (donations) {
try {
donations = JSON.parse(donations);
} catch (e) {
donations = [];
}
}
return {
sortType: "recent",
donations: [createDonation(12, "This is a comment"), createDonation(15)]
donations: donations || []
};
},
computed: {
Expand Down Expand Up @@ -66,6 +78,17 @@ export default {
//if (index !== -1) {
this.donations.splice(index, 1);
//}
},
save() {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(this.donations));
}
},
watch: {
donations: {
handler() {
this.save();
},
deep: true
}
}
};
Expand Down

0 comments on commit fb65a44

Please sign in to comment.