Created
November 14, 2019 03:00
-
-
Save bayareawebpro/91d6e1e8d1d9ff1419b3218290744848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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