亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

nodejs個(gè)人博客開發(fā)第五步 分配數(shù)據(jù)

 更新時(shí)間:2017年04月12日 12:00:09   作者:陶士涵  
這篇文章主要為大家詳細(xì)介紹了nodejs個(gè)人博客開發(fā)的分配數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了nodejs個(gè)人博客開發(fā)的分配數(shù)據(jù),具體內(nèi)容如下

使用回掉大坑進(jìn)行取數(shù)據(jù)

能看明白的就看,看不明白的手動(dòng)滑稽

/**
* 首頁(yè)控制器
*/
var router=express.Router();
/*每頁(yè)條數(shù)*/
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");
  // 分類數(shù)據(jù)
  categoryModel.getAllList(function(err,categoryList){
    // 文章條數(shù)
    articleModel.getCount(cid,function(err,nums){
      // 文章分頁(yè)
      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));
          }
          /*分配數(shù)據(jù)*/
          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":"測(cè)試"});
  //F.model("category").saveCate({"name":"測(cè)試1"},"id=4");
  //F.model("category").delCate("id=4");
  /*渲染模板*/
  //res.render("home/index");
});
module.exports=router;

文章模型:

/**
* 文章模型文件
*/
module.exports={
  /*獲取條數(shù)*/
  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);
  },
  /*獲取分頁(yè)數(shù)據(jù)*/
  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);
  }
};

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論