socialforge/public/javascripts/wechat/controllers/edit_class.js

112 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-07-04 17:04:06 +08:00
app.controller('EditClassController', ['$scope', '$http', 'auth', 'config', 'alertService','$location','$routeParams','rms','common', function($scope, $http, auth, config, alertService, $location,$routeParams, rms,common){
common.checkLogin();
2016-07-04 17:04:06 +08:00
var vm = $scope;
vm.syllabus = rms.get('current_edit_syllobus');
var syllabus_id = $routeParams.id;
if(!vm.syllabus){
$http.get(config.apiUrl+"syllabuses/"+syllabus_id+"?token="+auth.token()).then(function(response){
console.log(response.data);
vm.syllabus = response.data.data;
if (vm.syllabus) {
for (var i in vm.syllabus.courses) {
var course = vm.syllabus.courses[i];
course.tmpname = course.name;
}
vm.syllabus.tmptitle = vm.syllabus.title;
}
2016-07-04 17:04:06 +08:00
});
}
console.log(vm.syllabus);
2016-07-04 17:04:06 +08:00
//名字用两份
if (vm.syllabus) {
for (var i in vm.syllabus.courses) {
var course = vm.syllabus.courses[i];
course.tmpname = course.name;
}
vm.syllabus.tmptitle = vm.syllabus.title;
}
//单弹框
vm.alertService_1 = alertService.create();
//双弹框
vm.alertService_2 = alertService.create();
2016-07-04 17:04:06 +08:00
vm.addClass = function(){
vm.syllabus.courses.push({can_setting:true});
2016-07-04 17:04:06 +08:00
};
vm.deleteClass = function(index){
var course = vm.syllabus.courses[index];
if(course.id >0){
vm.alertService_2.showMessage('提示', '您确定要删除该班级吗?', function() {
$http.post(config.apiUrl+'courses/'+course.id+'/del', {
token: auth.token()
}).then(function(response){
if(response.data.status!=0){
2016-07-27 14:30:19 +08:00
vm.alertService_1.showMessage('提示', response.data.message);
} else {
vm.alertService_1.showMessage('提示', '删除班级成功', function(){
vm.syllabus.courses.splice(index, 1);
});
}
console.log(response.data.data);
});
2016-07-04 17:04:06 +08:00
});
2016-07-04 17:04:06 +08:00
} else {
vm.syllabus.courses.splice(index, 1);
}
}
vm.newClass = function (frm, syllabus) {
frm.$setSubmitted();
console.log(syllabus);
if(!frm.$valid){
console.log(frm.$error);
return;
}
var add_courses = [];
var modify_courses = [];
2016-07-04 17:04:06 +08:00
for(var i in vm.syllabus.courses){
var course = vm.syllabus.courses[i];
if(!course.id){
add_courses.push(course.tmpname);
}
else if(course.tmpname != course.name){
modify_courses.push({id:course.id,name:course.tmpname});
2016-07-04 17:04:06 +08:00
}
}
$http.post(config.apiUrl+'syllabuses/'+syllabus_id+'/edit', {
2016-07-04 17:04:06 +08:00
token: auth.token(),
id: syllabus_id,
title:vm.syllabus.tmptitle,
add_courses: add_courses,
modify_courses: modify_courses
2016-07-04 17:04:06 +08:00
}).then(function(response){
if(response.data.status!=0){
vm.alertService_1.showMessage('出错了', response.data.message);
2016-07-04 17:04:06 +08:00
} else {
vm.alertService_1.showMessage('提示', '保存课程成功', function(){
// window.history.back();
rms.save('syllabuses',[]);
$location.path("/class_list");
2016-07-04 17:04:06 +08:00
});
}
console.log(response.data.data);
});
}
}] );