code.js 986 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Or use axios directly instead of this.$http.
  2. Vue.prototype.$http = axios;
  3. class Errors {
  4. constructor() {
  5. this.clear();
  6. }
  7. any() {
  8. return Object.keys(this.errors).length != 0;
  9. }
  10. clear(name) {
  11. console.log("clearing", name);
  12. this.errors = {};
  13. }
  14. get(field) {
  15. if (this.errors[field]) {
  16. return this.errors[field];
  17. }
  18. }
  19. has(field) {
  20. return this.errors.hasOwnProperty(field);
  21. }
  22. record(errors) {
  23. this.errors = errors;
  24. }
  25. }
  26. const app = new Vue({
  27. el: '#root',
  28. data: {
  29. name: '',
  30. description: '',
  31. errors: new Errors(),
  32. },
  33. methods: {
  34. /**
  35. *
  36. * @param {Event} e
  37. */
  38. onSubmit(e) {
  39. // or use @submit.prevent at the HTML call point
  40. // e.preventDefault();
  41. this.$http.post('index.php/projects', this.$data)
  42. .then(this.onSuccess)
  43. .catch(error => this.errors.record({'name': error.response.data}));
  44. },
  45. onSuccess() {
  46. alert('Success');
  47. }
  48. }
  49. });