Commit | Line | Data |
---|---|---|
8a2b3260 BA |
1 | new Vue({ |
2 | el: '#courseList', | |
3 | data: { | |
4 | courseArray: courseArray, | |
5 | newCourse: { | |
6 | code: "", | |
7 | description: "", | |
e99c53fb | 8 | }, |
8a2b3260 BA |
9 | }, |
10 | mounted: function() { | |
11 | $('.modal').modal(); | |
12 | }, | |
13 | methods: { | |
14 | redirect: function(code) { | |
15 | document.location.href = "/" + initials + "/" + code; | |
e99c53fb | 16 | }, |
8a2b3260 BA |
17 | addCourse: function() { |
18 | if (!admin) | |
19 | return; | |
20 | // modal, fill code and description | |
21 | let error = Validator.checkObject({code:this.newCourse.code}, "Course"); | |
22 | if (!!error) | |
23 | return alert(error); | |
24 | else | |
25 | $('#newCourse').modal('close'); | |
73609d3b | 26 | $.ajax("/courses", |
8a2b3260 | 27 | { |
73609d3b | 28 | method: "POST", |
8a2b3260 BA |
29 | data: this.newCourse, |
30 | dataType: "json", | |
31 | success: res => { | |
32 | if (!res.errmsg) | |
33 | { | |
34 | this.newCourse["code"] = ""; | |
35 | this.newCourse["description"] = ""; | |
36 | this.courseArray.push(res); | |
37 | } | |
38 | else | |
39 | alert(res.errmsg); | |
40 | }, | |
41 | } | |
42 | ); | |
43 | }, | |
44 | deleteCourse: function(course) { | |
45 | if (!admin) | |
46 | return; | |
47 | if (confirm("Delete course '" + course.code + "' ?")) | |
73609d3b | 48 | $.ajax("/courses", |
e99c53fb | 49 | { |
73609d3b | 50 | method: "DELETE", |
8a2b3260 | 51 | data: { cid: course._id }, |
e99c53fb BA |
52 | dataType: "json", |
53 | success: res => { | |
54 | if (!res.errmsg) | |
8a2b3260 BA |
55 | this.courseArray.splice( this.courseArray.findIndex( item => { |
56 | return item._id == course._id; | |
57 | }), 1 ); | |
e99c53fb BA |
58 | else |
59 | alert(res.errmsg); | |
60 | }, | |
61 | } | |
62 | ); | |
63 | }, | |
8a2b3260 BA |
64 | } |
65 | }); |