Skip to content

Instantly share code, notes, and snippets.

@bayareawebpro
Created November 14, 2019 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayareawebpro/91d6e1e8d1d9ff1419b3218290744848 to your computer and use it in GitHub Desktop.
Save bayareawebpro/91d6e1e8d1d9ff1419b3218290744848 to your computer and use it in GitHub Desktop.
<script>
import {Validator} from 'laravel-micro.js'
export default {
name: "Form",
props:{
route: {
type: String,
required: true,
},
method: {
type: String,
default: ()=> 'post'
},
data: {
type: Object,
default: ()=> ({})
}
},
data(){
return {
form: this.data,
isLoading: false,
validator: new Validator,
}
},
methods:{
submit() {
this.isLoading = true
this.$options.$http
.request(this.route, {
data: this.form,
method: this.method
}).then(({data}) => {
this.form = Object.assign({}, this.form, data)
this.$emit('saved', this.form)
}).catch(({response}) => {
this.validator.sync(response.data)
this.$emit('error', this.validator)
}).finally(() => {
setTimeout(()=>{
this.isLoading = false
}, 1000)
})
},
},
provide(){
return {
form: this.form,
submit: this.submit,
validator: this.validator,
loading: ()=>this.isLoading,
}
},
created() {
this.$options.$http = this.$app.make('Http')
}
}
</script>
<template>
<form ref="form" @submit.prevent>
<slot></slot>
</form>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment