Skip to content

Instantly share code, notes, and snippets.

@dannyrb
Last active May 27, 2018 03:07
Show Gist options
  • Save dannyrb/59ebb502b93bdcba940c1614cb20c771 to your computer and use it in GitHub Desktop.
Save dannyrb/59ebb502b93bdcba940c1614cb20c771 to your computer and use it in GitHub Desktop.
Example: Basic v-cache configuration
<template><!-- Business as usual --></template>
<script>
export default {
name: 'BasicForm',
data() {
return {
// Default setup uses 'form' object in
// data as cache target and restore cache target
form: {
name: '',
email: '',
message: '',
termsAccepted: false
}
}
},
watch: {
// Watch for changes to all of form's properties
// and trigger caching behavior
form: {
handler: function() {
this.$emit("cache", this.form);
},
deep: true
}
},
mounted() {
// After this form is rendered, restore its form values
// if it has any cached form values
this.$emit("restore-cache");
},
methods: {
pretendSubmitForm() {
this.form = {
username: "",
email: "",
message: "",
termsAccepted: false
};
// Clear any cached values for this form
this.$emit("clear-cache");
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment