【9】添加主页日志列表展示
分支:home_page
预览图:
index.htm
1 {% extends "base.htm" %} 2 3 {%block header%} 4 {%include 'header.htm'%} 5 {%end%} 6 7 {%block content%} 8 <script src="{{ static_url('js/knockout/knockout-2.1.0.js') }}"></script> 9 <script src="{{ static_url('js/knockout/knockout.mapping-2.4.1.js') }}"></script> 10 <script src="{{ static_url('js/components/jquery.twbsPagination.min.js') }}"></script> 11 <script src="{{ static_url('js/index.js') }}"></script> 12 13 <div class="bs-docs-container"> 14 <div class="container"> 15 <div class="row"> 16 <div class="col-md-9 blog-main"> 17 <div style="min-height: 650px"> 18 <!-- ko foreach: $data --> 19 <div class="blog-post"> 20 <button style="margin-left: -15px;font-size: 30px" class="btn btn-link" id="blogTitleBtn" data-bind="text: blog_title, click:function(name){show_blog(name)}"></span></button> 21 22 <p style="font-size: 20px" data-bind="text: blog_summary"></p> 23 24 <p class="blog-post-meta" data-bind="text: show_author_info(user_name, created_at)" style="font-size: 15px;"></p> 25 26 <hr> 27 </div> 28 <!-- /ko --> 29 </div> 30 31 <div id="pagination_box" class="pull-right"> 32 <ul id="pagination_zc" class="pagination-sm"></ul> 33 </div> 34 </div> 35 36 <div class="col-md-3"> 37 <div class="blog-header"> 38 <h1 class="blog-title">Jakey's Blog</h1> 39 <p class="lead blog-description">Test</p> 40 </div> 41 <hr> 42 <div class="list-group"> 43 <a href="#" class="list-group-item active">列 表 A</a> 44 <a href="#" class="list-group-item">子列表1</a> 45 <a href="#" class="list-group-item">子列表2</a> 46 <a href="#" class="list-group-item">子列表3</a> 47 <a href="#" class="list-group-item">子列表4</a> 48 </div> 49 <hr> 50 <div class="list-group"> 51 <a href="#" class="list-group-item active">列 表 B</a> 52 <a href="#" class="list-group-item">子列表1</a> 53 <a href="#" class="list-group-item">子列表2</a> 54 <a href="#" class="list-group-item">子列表3</a> 55 <a href="#" class="list-group-item">子列表4</a> 56 <a href="#" class="list-group-item">子列表5</a> 57 <a href="#" class="list-group-item">子列表6</a> 58 <a href="#" class="list-group-item">子列表7</a> 59 <a href="#" class="list-group-item">子列表8</a> 60 </div> 61 </div> 62 </div> 63 </div> 64 </div> 65 {%end%} 66 67 {%block footer%} 68 {%include 'footer.htm'%} 69 {%end%}
index.js
1 /** 2 * 初始化时加载 3 */ 4 // ko数据绑定 5 var model; 6 var viewModel; 7 8 // 分页 9 var page_size = 1 10 var total_pages = 1; 11 var visiblePages = 8; 12 13 $(document).ready(function () { 14 get_all_blogs(1); 15 }); 16 17 /** 18 * 分页 19 */ 20 function twbsPagination() { 21 $('#pagination_zc').twbsPagination({ 22 totalPages: total_pages, 23 visiblePages: visiblePages > 8 ? 8 : visiblePages, 24 startPage: 1, 25 first: "首页", 26 prev: "上一页", 27 next: "下一页", 28 last: "未页", 29 onPageClick: function (event, page) { 30 get_all_blogs(page); 31 } 32 }); 33 } 34 35 /** 36 * 获取所有日志信息 37 */ 38 function get_all_blogs(current_page) { 39 $.ajax({ 40 type: "POST", 41 url: "/manage/blogs/query_all_blogs", 42 data: { 43 "_xsrf": getCookie("_xsrf"), 44 "current_page": current_page 45 }, 46 success: function (data) { 47 if(data["status"] == "success"){ 48 total_pages = parseInt(data["info"].total_pages); 49 if(total_pages == 0) 50 { 51 total_pages = 1; 52 } 53 54 if(viewModel == undefined){ 55 viewModel = ko.mapping.fromJS(data["info"].current_data); 56 model = ko.toJS(viewModel); 57 ko.applyBindings(viewModel); 58 twbsPagination(); 59 } 60 else{ 61 if(page_size != total_pages){ 62 $('#pagination_zc').remove(); 63 $("#pagination_box").append('<ul id="pagination_zc" class="pagination-sm"></ul>'); 64 twbsPagination(); 65 } 66 ko.mapping.fromJS(data["info"].current_data, viewModel); 67 model = ko.toJS(viewModel); 68 } 69 $("#table_title").html("日 志 列 表 (" + data["info"].total_count + ")"); 70 page_size = total_pages; 71 } 72 }, 73 error : function() { 74 errNotify("可能为网络异常,请检查您的网络!"); 75 } 76 }); 77 } 78 79 /** 80 * 显示博客信息 81 */ 82 function show_blog(name) { 83 console.log("待完成"); 84 console.log(name.id()); 85 console.log(name.blog_title()); 86 } 87 88 /** 89 * 编辑博客 90 */ 91 function edit_blog(name) { 92 window.location.href = "/blog/edit/info?id=" + name.id(); 93 } 94 95 /** 96 * 删除博客 97 */ 98 function delete_blog(name) { 99 $.messager.model = { 100 ok:{ text: "残忍删除", classed: 'btn-info' }, 101 cancel: { text: "勉强挽留", classed: 'btn-default' } 102 }; 103 104 $.messager.confirm("删除日志", "确定删除此日志吗?", function() { 105 var blog_id = name.id(); 106 $.ajax({ 107 type: "POST", 108 url: "/manage/blogs/delete_blog", 109 data: { 110 "_xsrf": getCookie("_xsrf"), 111 "blog_id": blog_id 112 }, 113 success: function (data) { 114 if(data["status"] == "error"){ 115 errNotify("删除错误!\r\n 请检查网络状态是否正常!"); 116 } 117 else{ 118 viewModel.remove(name); 119 model = ko.toJS(viewModel); 120 successNotify("删除成功!"); 121 } 122 }, 123 error : function() { 124 errNotify("error!"); 125 } 126 }); 127 }); 128 } 129 130 /** 131 * 显示作者信息 132 */ 133 function show_author(name) { 134 console.log("待完成"); 135 } 136 137 138 /** 139 * 显示日志简介信息 140 */ 141 function show_author_info(user_name, created_at) { 142 var author_lst = new Array("作者 ", user_name(), " | 发布于 ", created_at()); 143 return author_lst.join(""); 144 }
manage.py注释掉 BlogsHandler post 的 @tornado.web.authenticated,希望没有登陆的用户也能看到日志信息。