productsDetail.ejs 3.3 KB
<% include ../includes/header.ejs %>
    <div class="panel panel-default">
        <div class="panel-heading">
            <%=product.name%>
        </div>
        <div class="panel-body">
            <div style="padding-bottom: 10px">
                작성일 : 
                <%=product.getDate.year%> - 
                <%=product.getDate.month%> - 
                <%=product.getDate.day%>
            </div>
            <% if(product.thumbnail){%>
                <p>
                    <img src="/uploads/<%=product.thumbnail%>" style="max-width: 100%"/>
                </p>
            <% } %>
            <%=product.description%>
            <!-- 댓글영역  -->
            <div>
                댓글작성하기
                <form id="commentForm" action="" method="post">
                    <input type="hidden" name="product_id" value="<%=product.id%>" />
                    <textarea class="form-control" name="content"></textarea>
                    <button class="btn btn-primary" style="margin-top: 10px">댓글작성</button>
                </form>
            </div>
            <!-- 댓글영역  -->
            <hr />
            <div id="comment_area">
                <% comments.forEach(function(comment){ %>
                    <div>
                        <%=comment.content%>
                        ( <a class='comment_delete' comment_id='<%=comment.id%>'>삭제</a> )
                    </div>
                <% }); %>
            </div>
        </div>
    </div>
 
    <a href="/admin/products" class="btn btn-default">목록으로</a>
    <a href="/admin/products/edit/<%=product.id%>" class="btn btn-primary">수정</a>
<% include ../includes/footer.ejs %>
<script>
(function(){
    $(document).ready(function() {
        $('#commentForm').submit(function(){
            var $contentVal = $(this).children('textarea[name=content]').val();
            if($contentVal){
                $.ajax({
                    url: '/admin/products/ajax_comment/insert',
                    type: 'POST',
                    data: $(this).serialize(),
                })
                .done(function(args) {
                    if(args.message==="success"){
                        $('#comment_area').append(
                            '<div>' + args.content +
                            " ( <a class='comment_delete' comment_id='"+ args.id +"'>삭제</a> ) </div>"
                        );
                        $('#commentForm textarea[name=content]').val("");
                    }
                })
                .fail(function(args) {
                    console.log(args);
                });
            }else{
                alert('댓글 내용을 입력해주세요.')
            }
            return false;
        });
    });
})();
</script>
<script>
$(document).on('click' , '.comment_delete' , function(){
    if(confirm('삭제하시겠습니까?')){ //확인창 예 눌렀을 시만 진행
        var $self = $(this);
        $.ajax({
            url: '/admin/products/ajax_comment/delete',
            type: 'POST',
            data: { comment_id : $self.attr('comment_id') },
        })
        .done(function() {
            $self.parent().remove();
            alert("삭제가 완료되었습니다.");
        })
        .fail(function(args) {
            console.log(args);
        });
    }
});
</script>