api.js
var api = {}
api.ajax = function(url, method, form, success, error) {
var request = {
url: url,
type: method,
data: form,
success: function(response) {
var r = JSON.parse(response)
success(r)
},
error: function(err) {
log('网络错误', error)
var r = {
'success': false,
message: '网络错误'
}
error(r)
}
}
$.ajax(request)
}
api.ajax = function(url, method, form, success, error) {
var request = {
url: url,
type: method,
data: form,
success: function(response) {
var r = JSON.parse(response)
success(r)
},
error: success(err) {
log('网络错误', error)
error(r)
}
}
$.ajax(request)
}
api.get = function(url, success, error) {
api.ajax(url, 'get', {}, success, error)
}
api.post = function(url, form, response) {
api.ajax(url, 'post', form, response, response)
}
// weibo API
api.weiboAdd = function(form, response) {
var url = '/api/weibo/add'
api.post(url, form, response)
}
api.weiboDelete = function(weiboId, success, error) {
var url = '/api/weibo/delete/' + weiboId
var form = {}
api.get(url, success, error)
}
// 评论 API
// 用户 API
// window.onload = main;
var log = function() {
console.log(arguments)
}
var weiboTemplate = function(weibo) {
var w = weibo
var t = `
<div class="weibo-cell cell item">
<img src="${ w.avatar }" class="avatar">
<span>${ w.weibo }</span>
<span class="right span-margin">by: ${ w.name }</span>
<span class="right span-margin">${ w.created_time }</span>
<div class="right span-margin">
<button class="weibo-delete" data-id="${ w.id }">删除</button>
<a href="#" class="com">评论(${ w.comments_num })</a>
</div>
<div class="comment-div hide">
<div class="">
</div>
<input type="hidden" name="weibo_id" value="${ w.id }">
<input name="comment" class="left m" placeholder="Comment">
<button>发表</button>
</div>
</div>
`
return t
}
$(document).ready(function(){
// 展开评论事件
$('a.com').on('click', function(){
$(this).parent().next().slideToggle()
return false;
})
// 绑定删除微博按钮事件,事件委托
$('.weibo-container').on('click', '.weibo-delete', function(){
var weiboId = $(this).data('id')
log(weiboId)
var weiboCell = $(this).closest('.weibo-cell')
var success = function(response) {
console.log('成功', arguments)
$(weiboCell).slideUp()
alert("删除成功")
// $('.weibo-container').prepend(weiboTemplate(w))
}
var error = function() {
console.log('错误', arguments)
alert("删除失败")
}
api.weiboDelete(weiboId, success, error)
})
// 给按钮绑定添加 weibo 事件
$('#id-button-weibo-add').on('click', function(){
var weibo = $('#id-input-weibo').val()
log('weibo,', weibo)
var form = {
weibo: weibo,
}
var response = function(r) {
console.log('成功', arguments)
log(r)
if(r.success) {
var w = r.data
$('.weibo-container').prepend(weiboTemplate(w))
alert("添加成功")
} else {
alert(r.message)
}
}
api.weiboAdd(form, response)
})
})
/*
function main(){
$('a.com').on('click', function(){
$(this).parent().next().slideToggle()
return false;
})
$('a.blog-com').on('click', function(){
$(this).parent().next().slideToggle()
return false;
})
$('.blog-comment-add').on('click', function(){
console.log('add button')
var button = $(this)
var parent = button.parent()
var blog_id = parent.find('.comment-blog_id').val()
console.log('weibo', blog_id)
var comment = parent.find('.comment-content').val()
console.log('comment', comment)
var commentList = parent.parent().find('.comment-list')
console.log('commentList', commentList)
var weibo = {
'blog_id': blog_id,
'comment': comment
}
var request = {
url: '/blog/comment',
type: 'post',
data: weibo,
success: function() {
console.log('成功', arguments)
var response = arguments[0]
var comment = JSON.parse(response)
var content = comment.comment
var avatar = comment.avatar
var created_time = comment.created_time
var name = comment.name
var cell = `
<div class="cell-inner item">
<img src="${avatar}" class="avatar-s">
<span class="comment">${content}</span>
<span class="time right span-margin">${created_time}</span>
<span class="name right span-margin">by:${name}</span>
</div>
`;
commentList.append(cell)
parent.find('.comment-content').val("")
},
error: function() {
console.log('错误', arguments)
}
}
$.ajax(request)
})
}
*/
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/css/pure-min.css">
<link rel="stylesheet" href="/static/css/main.css">
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="/static/js/api.js"></script>
<script src="/static/js/weibo.js"></script>
<title>三千院和他的朋友们</title>
</head>
<body>
<div class="head">
<div class="left title">三千院和他的朋友们</div>
<div class="right">
<span><a href="/blog">三千院的博客</a></span>
<span><a href="/weibo">微博</a></span>
<span><a href="/profile">个人资料</a></span>
</div>
</div>
<div class="background">
<div class="height-20"></div>
<div class="content clearfix">
<div class="box add-box">
<!-- <form action="/add" method="post" class="out"> -->
<input id="id-input-weibo" name="weibo" class="wei" placeholder="Weibo"> </input>
<br>
<button id="id-button-weibo-add" class="pure-button pure-button-primary sub"
type="submit">水一发!</button>
<!-- </form> -->
</div>
<div class="height-20"></div>
<div class="box weibo-container">
{% for t in weibos %}
<div class="weibo-cell cell item">
<img src="{{ t.avatar }}" class="avatar">
<span>{{ t.weibo }}</span>
<span class="right span-margin">{{ t.created_time }}</span>
<span class="right span-margin">by: {{ t.name }}</span>
<div class="right span-margin">
<button class="weibo-delete" data-id="{{ t.id }}">删除</button>
<a href="#" class="com">评论({{ t.comments_num }})</a>
</div>
<div class="comment-div hide">
<div class="">
{% for c in t.comment %}
<div class="cell-inner item">
<img src="{{ c.avatar }}" class="avatar-s">
<span class="comment">{{ c.comment }}</span>
<span class="time right span-margin">{{ c.created_time }}</span>
<span class="name right span-margin">by:{{ c.name }}</span>
</div>
{% endfor %}
</div>
<form action="/comment" method="post" class="comment-form">
<input type="hidden" name="weibo_id" value="{{ t.id }}">
<input name="comment" class="left m" placeholder="Comment">
<button>发表</button>
</form>
</div>
</div>
{% endfor %}
</div>
<div class="height-20"></div>
</div>
</div>
</body>
</html>
网友评论