Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Form {
- constructor (data) {
- this._originalData = Object.assign({}, data)
- for (let key in data) {
- this[key] = data[key]
- }
- }
- reset () {
- for (let key in this._originalData) {
- this[key] = this._originalData[key]
- }
- }
- clear () {
- for (let key in this._originalData) {
- this[key] = null
- }
- }
- async submit (method, url) {
- const response = await fetch(url, {
- method,
- headers: {
- 'Accept': 'application/json; charset=utf-8',
- 'Content-Type': 'application/json; charset=utf-8',
- },
- body: JSON.stringify(this.data),
- })
- return await response.json()
- }
- async post (url) {
- return await this.submit('POST', url)
- }
- async put (url) {
- return await this.submit('PUT', url)
- }
- async delete (url) {
- return await this.submit('DELETE', url)
- }
- }
Add Comment
Please, Sign In to add comment