script.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var http = new HttpService('http://helloworld.filonitta.fe4.a-level.com.ua/api/v1');
  2. console.log(http);
  3. var $studentList = $('#students-list');
  4. var $formEditStudent = $('#form-edit-student');
  5. var request = http.getStudents();
  6. var $formCreateStudent = $('#form-create-student');
  7. var $addField = $('#add-field').data('counter', 1);
  8. request.done(function(response){
  9. console.log(response);
  10. response.forEach(function(student){
  11. $('<a href="">')
  12. .text(student.lastname + ' ' + student.firstname)
  13. .attr('data-id', student.id)
  14. .addClass('list-group-item')
  15. .appendTo($studentList);
  16. })
  17. });
  18. request.fail(function(error){
  19. console.log(error);
  20. })
  21. $studentList.on('click', '[data-id]', function(event){
  22. event.preventDefault();
  23. var studentId = $(this).data('id');
  24. $('.has-error', $formEditStudent).removeClass('has-error');
  25. $('.has-error', $formCreateStudent).removeClass('has-error');
  26. $formEditStudent.find('[id="extraField"]').remove();
  27. http.getStudentsById(studentId)
  28. .done(function(response) {
  29. console.log(response);
  30. if (response.extraField) {
  31. for(var key in response.extraField) {
  32. var $extraField = $('<div>').addClass('form-group').appendTo($formEditStudent);;
  33. var $extraFieldInput = $('<input>')
  34. .attr({'type': 'text', 'name': 'extraField[' + key + ']', 'id': 'extraField'})
  35. .val(response.extraField[key])
  36. .addClass('form-control')
  37. .appendTo($extraField);
  38. }
  39. }
  40. for(var key in response) {
  41. $formEditStudent.find('[name="' + key + '"]').val(response[key]);
  42. }
  43. })
  44. })
  45. $formEditStudent.on('submit', function(event){
  46. event.preventDefault();
  47. var that =this;
  48. $('.has-error', this).removeClass('has-error');
  49. var data = $(this).serialize(),
  50. studentId = $(this).find('[name=id]').val();
  51. http.updateUserById(studentId, data)
  52. .done(function(response){
  53. $studentList
  54. .find('[data-id="' + studentId + '"]')
  55. .text(response.lastname + ' ' + response.firstname);
  56. })
  57. .fail(function(response){
  58. var errors = response.responseJSON.errors;
  59. errors.forEach(function(error) {
  60. console.log(error.field);
  61. console.log(that);
  62. $(that)
  63. .find('[name="' + error.field + '"]')
  64. .parent()
  65. .addClass('has-error');
  66. })
  67. })
  68. })
  69. $formCreateStudent.on('submit', function(event){
  70. event.preventDefault();
  71. var data = $(this).serialize();
  72. var that =this;
  73. console.log(data);
  74. $('.has-error', this).removeClass('has-error');
  75. http.createStudent(data)
  76. .done(function(response){
  77. $('<a href="">')
  78. .text(response.lastname + ' ' + response.firstname)
  79. .attr('data-id', response.id)
  80. .addClass('list-group-item')
  81. .appendTo($studentList);
  82. })
  83. .fail(function(response){
  84. var errors = response.responseJSON.errors;
  85. errors.forEach(function(error) {
  86. $(that)
  87. .find('[name="' + error.field + '"]')
  88. .parent()
  89. .addClass('has-error');
  90. })
  91. })
  92. })
  93. $addField.on('click', function(event){
  94. var counter = $addField.data('counter');
  95. $addField.data('counter', counter + 1);
  96. var $extraField = $('<div>').addClass('form-group').appendTo($formCreateStudent);;
  97. var $extraFieldInput = $('<input>')
  98. .attr({'type': 'text', 'name': 'extraField[' + counter + ']', 'placeholder': 'field' + counter})
  99. .addClass('form-control')
  100. .appendTo($extraField);
  101. })