[nodejs] nodejs开发个人博客(五)分配数据

使用回掉大坑进行取数据

能看明白的就看,看不明白的手动滑稽

/**
* 首页控制器
*/
var router=express.Router();
/*每页条数*/
var pageSize=5; router.get('/',function(req,res,next){
var currentPage=parseInt(req.params.page);
var cid=0; var categoryModel=F.model("category");
var articleModel=F.model("article");
// 分类数据
categoryModel.getAllList(function(err,categoryList){
// 文章条数
articleModel.getCount(cid,function(err,nums){
// 文章分页
articleModel.getArticlePager(cid,currentPage,pageSize,function(err,articleList){
var nextPage=(currentPage+1)>=Math.ceil(nums[0].num/pageSize) ? Math.ceil(nums[0].num/pageSize) : currentPage+1;
var prePage=(currentPage-1)<=0 ? 1 : currentPage-1;
// 归档
articleModel.getArchives(function(err,allArticleTime){
var newArticleTime=[];
for(var i=0;i<allArticleTime.length;i++){
newArticleTime.push(F.phpDate("y年m月",allArticleTime[i].time));
}
/*分配数据*/
var data={
categoryList:categoryList,
articleList:articleList,
cid:cid,
nextPage:nextPage==0 ? 1 : nextPage,
prePage:prePage,
allArticleTime:newArticleTime,
currentPage:currentPage
}; /*渲染模板*/
res.render("home/index",data);
});
});
}); }); //F.model("category").addCate({"name":"测试"});
//F.model("category").saveCate({"name":"测试1"},"id=4");
//F.model("category").delCate("id=4");
/*渲染模板*/
//res.render("home/index");
});
module.exports=router;

文章模型:

/**
* 文章模型文件
*/
module.exports={
/*获取条数*/
getCount:function(categoryId,callback){
var condition="";
if(categoryId!=0){
condition="where category_id="+categoryId;
}
var sql="select count(*) num from article "+condition;
db.query(sql,callback);
},
/*获取分页数据*/
getArticlePager:function(categoryId,currentPage,pageSize,callback){
if(currentPage<=0||!currentPage) currentPage=1;
var start=(currentPage-1)*pageSize;
var end=pageSize;
var condition="";
if(categoryId!=0){
condition="where category_id="+categoryId;
}
var sql="select * from article "+condition+" order by time desc limit "+start+","+end;
db.query(sql,callback);
},
/*归档*/
getArchives:function(callback){
db.query("select time from article order by time desc",callback);
}
};
上一篇:十大免费SSL证书:网站免费添加HTTPS加密


下一篇:MogileFS-2.44 安装与配置