vueGroup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://schedule.tomtit.tomsk.ru/api'
  3. method = method.toLocaleUpperCase()
  4. let fullurl = `${apihost}${url}`;
  5. let options = {
  6. method: method,
  7. headers: {
  8. "Content-Type": "application/json",
  9. "Authorization": `Bearer ${token}`,
  10. },
  11. };
  12. switch(method) {
  13. case "PUT":
  14. delete options.headers["Content-Type"];
  15. options.body = data;
  16. break;
  17. case "POST": case "PATCH": case "DELETE":
  18. options.body = JSON.stringify(data);
  19. break;
  20. }
  21. const res = await fetch(fullurl, options);
  22. return await res.json();
  23. };
  24. var app = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. groups:[],
  29. teachers:[],
  30. selected:null
  31. },
  32. methods: {
  33. showGroups() {
  34. console.log(this.groups)
  35. },
  36. async getGroups(){
  37. this.groups = await requestFunc("/group/", "GET")
  38. },
  39. async getTeachers(){
  40. teachers = await requestFunc("/teacher/", "GET")
  41. for(let i =0; i < teachers.length; i++) {
  42. Vue.set(this.teachers, i, teachers[i]);
  43. }
  44. },
  45. async mountFunc(){
  46. await this.getTeachers()
  47. await this.getGroups()
  48. },
  49. },
  50. async mounted() {
  51. await this.mountFunc()
  52. console.log(this.teachers)
  53. }
  54. });
  55. // const requestFunc = async(url, method = "GET", data = null, token = null) => {
  56. // apihost = 'http://schedule.tomtit.tomsk.ru/api'
  57. // method = method.toLocaleUpperCase()
  58. // let fullurl = `${apihost}${url}`;
  59. // let options = {
  60. // method: method,
  61. // headers: {
  62. // "Content-Type": "application/json",
  63. // "Authorization": `Bearer ${token}`,
  64. // },
  65. // };
  66. // switch(method) {
  67. // case "PUT":
  68. // delete options.headers["Content-Type"];
  69. // options.body = data;
  70. // break;
  71. // case "POST": case "PATCH": case "DELETE":
  72. // options.body = JSON.stringify(data);
  73. // break;
  74. // }
  75. // const res = await fetch(fullurl, options);
  76. // return await res.json();
  77. // };
  78. // var app = new Vue({
  79. // el: '#app',
  80. // delimiters: ['${', '}'],
  81. // data: {
  82. // groups:[],
  83. // teachers:[],
  84. // profmoduls:[],
  85. // },
  86. // methods: {
  87. // showGroups() {
  88. // console.log(this.groups)
  89. // },
  90. // async getGroups(){
  91. // this.groups = await requestFunc("/group/", "GET")
  92. // },
  93. // async getTeachers(){
  94. // teachers = await requestFunc("/teacher/", "GET")
  95. // for(let i =0; i < teachers.length; i++) {
  96. // Vue.set(this.teachers, i, teachers[i]);
  97. // },
  98. // },
  99. // async mountFunc(){
  100. // await this.getTeachers()
  101. // await this.getGroups()
  102. // },
  103. // },
  104. // async mounted() {
  105. // await this.mountFunc()
  106. // console.log(this.teachers)
  107. // }
  108. // });