本文为大家分享了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);
}
};
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。