Guest User

Untitled

a guest
Dec 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class Form {
  2.  
  3. constructor (data) {
  4. this._originalData = Object.assign({}, data)
  5.  
  6. for (let key in data) {
  7. this[key] = data[key]
  8. }
  9. }
  10.  
  11. reset () {
  12. for (let key in this._originalData) {
  13. this[key] = this._originalData[key]
  14. }
  15. }
  16.  
  17. clear () {
  18. for (let key in this._originalData) {
  19. this[key] = null
  20. }
  21. }
  22.  
  23. async submit (method, url) {
  24. const response = await fetch(url, {
  25. method,
  26. headers: {
  27. 'Accept': 'application/json; charset=utf-8',
  28. 'Content-Type': 'application/json; charset=utf-8',
  29. },
  30. body: JSON.stringify(this.data),
  31. })
  32.  
  33. return await response.json()
  34. }
  35.  
  36. async post (url) {
  37. return await this.submit('POST', url)
  38. }
  39.  
  40. async put (url) {
  41. return await this.submit('PUT', url)
  42. }
  43.  
  44. async delete (url) {
  45. return await this.submit('DELETE', url)
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment