由于都是ajax,所以 采用几个js文件关联 ajax接收文件(ajax.js): var FullCalendar = require(“./…/moudels/fullCalendar.js”); module.exports = function(app){ app.post("/insertCalendar",filter.authorize ,function(req,res){ FullCalendar.insertCalendar(req,res,function(){ res.send({status:0}); }); }); app.post("/findCalendar",filter.authorize ,function(req,res){ FullCalendar.findCalendar(req,res,function(data){ var dat = []; for(i in data){ var start = data[i].start; var end = data[i].end; start.setDate(start.getDate());//解决客户端日期BUG +1 end.setDate(end.getDate());
var y_start = start.getFullYear(),m_start = start.getMonth(),d_start = start.getDate(),hh_start = start.getHours(),mm_start = start.getMinutes();
var y_end = end.getFullYear(),m_end = end.getMonth(),d_end = end.getDate(),hh_end = end.getHours(),mm_end = end.getMinutes();
//start date对象给客户端传递,出现数据打乱。单个时间分钟传送
dat.push(
{_id:data[i]._id,
title:data[i].title,
y_start:y_start, m_start:m_start, d_start:d_start,hh_start : hh_start, mm_start: mm_start,
y_end :y_end, m_end:m_end, d_end: d_end, hh_end: hh_end,mm_end : mm_end,
/*start:new Date(y_start,m_start,d_start,hh_start,mm_start),
end:new Date(y_end,m_end,d_end,hh_end,mm_end),
start:start,
end:end,*/
allDay:data[i].allDay}
);
//console.log(hh_start +":"+mm_start + "*_*"+hh_end+":"+mm_end);
//console.log(start+"*_*"+end);
}
res.send({status:0,events:dat});
});
});
app.post("/updateCalendar",filter.authorize ,function(req,res){
FullCalendar.updateCalendar(req,res,function(){
res.send({status:0});
});
});
app.post("/updateDateCalendar",filter.authorize ,function(req,res){
FullCalendar.updateDateCalendar(req,res,function(){
res.send({status:0});
});
});
app.post("/updateDateTimeCalendar",filter.authorize ,function(req,res){
FullCalendar.updateDateTimeCalendar(req,res,function(){
res.send({status:0});
});
});
app.post("/updateTimeCalendar",filter.authorize ,function(req,res){
FullCalendar.updateTimeCalendar(req,res,function(){
res.send({status:0});
});
});
app.post("/removeCalendar",filter.authorize ,function(req,res){
FullCalendar.removeCalendar(req,res,function(){
res.send({status:0});
});
});
}
数据库处理文件(fullCalendar.js): var status = require(‘./…/status’); var mongoose = status.mongoose; var db = status.db; var Schema = mongoose.Schema;
var calendar = db.model('calendar’,new Schema({
user:String
,title:String
,url:String
,innerdate:{type:Date, default: Date.now}
,update:{type:Date, default: Date.now}
,start:{type:Date, default: Date.now}
,end:{type:Date, default: Date.now}
,allDay:Boolean
}));
var Calendar = function(){};
//添加新calendar Calendar.prototype.insertCalendar = function(req,res,func){ var func = func ||function(){} var start = new Date(req.body.start),end = new Date(req.body.end); //start.setHours(0); var cal = new calendar({user:req.session.name,title:req.body.title,url:req.body.url,innerdate:Date(),update:Date(),start:start,end:end,allDay:req.body.allDay}); cal.save(function(err){ if(err){ throw err; } func(req,res); }); }
//查找 Calendar.prototype.findCalendar = function(req,res,func){ var func = func ||function(){} calendar.find({user:req.session.name},function(err,data){ func(data); }); }
//标题更新 Calendar.prototype.updateCalendar = function(req,res,func){ var func = func ||function(){} calendar.update({user:req.session.name,_id:req.body._id},{$set:{ update:Date(),title:req.body.title}}, function(err){ if(err){ console.log(err); }
func();
}
);
}
//整天更新 Calendar.prototype.updateDateCalendar = function(req,res,func){ var func = func ||function(){} calendar.find({user:req.session.name,_id:req.body._id},function(err,data){ if(data.length>0){
var start = data[0].start,end = data[0].end;
start.setDate(start.getDate()+parseInt(req.body.dayDelta));
end.setDate(end.getDate()+parseInt(req.body.dayDelta));
calendar.update({user:req.session.name,_id:req.body._id},{$set:{
update:Date(),start:start,end:end,allDay:true}},
function(err){
if(err){
console.log(err);
}
func();
}
);
}
func();
});
}
//时间、天数更新 Calendar.prototype.updateDateTimeCalendar = function(req,res,func){ var func = func ||function(){} calendar.find({user:req.session.name,_id:req.body._id},function(err,data){ if(data.length>0){ var start = new Date(req.body.start);
if(data[0].allDay == true){
var dayDelta = parseInt(req.body.dayDelta);
var end = new Date(req.body.start);
end.setDate(start.getDate()),end.setHours(start.getHours() + 2);
}else{
var dayDelta = parseInt(req.body.dayDelta),minuteDelta = parseInt(req.body.minuteDelta);
var end = data[0].end;
//end.setDate(end.getDate()+dayDelta-1),end.setMinutes(end.getMinutes()+minuteDelta);
end.setDate(end.getDate()+dayDelta),end.setMinutes(end.getMinutes()+minuteDelta);
}
calendar.update({user:req.session.name,_id:req.body._id},{$set:{update:Date(),start:start,end:end,allDay:false}},function(err){
if(err){
console.log(err);
}
func(0);
});
}else{
func(1);
}
});
}
//时间更新 eventsize 也可能天数 Calendar.prototype.updateTimeCalendar = function(req,res,func){ var func = func ||function(){} calendar.find({user:req.session.name,_id:req.body._id},function(err,data){ if(data.length>0){ var dayDelta = parseInt(req.body.dayDelta),minuteDelta = parseInt(req.body.minuteDelta); var end = data[0].end; end.setDate(end.getDate()+dayDelta),end.setMinutes(end.getMinutes()+minuteDelta); if(parseInt(req.body.dayDelta) == 0){
calendar.update({user:req.session.name,_id:req.body._id},{$set:{update:Date(),end:end,allDay:false}},function(err){
if(err){
console.log(err);
}
func(0);
});
}else{
console.log("天数改变");
calendar.update({user:req.session.name,_id:req.body._id},{$set:{update:Date(),end:end,allDay:true}},function(err){
if(err){
console.log(err);
}
func(0);
});
}
}else{
func(1);
}
});
}
//删除单条calendar Calendar.prototype.removeCalendar = function(req,res,func){ var func = func ||function(){} calendar.remove({_id:req.body._id},function(err){ if(err){ throw err; } func(req,res); }); }
module.exports = new Calendar();
到此,交互功能完成,严格测试,火狐goole下完美运行,体验很爽