posting comments is now possible.

This commit is contained in:
nilsding 2014-12-07 15:05:44 +01:00
parent 50151453d8
commit 8ab5458b1b
4 changed files with 53 additions and 4 deletions

View File

@ -168,6 +168,53 @@ $(document).on "click", "button[name=ab-comments]", ->
commentBox.slideUp()
btn[0].dataset.state = 'hidden'
$(document).on "keyup", "input[name=ab-comment-new]", (evt) ->
input = $(this)
aid = input[0].dataset.aId
ctr = $("span#ab-comment-charcount-#{aid}")
cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]")
if evt.which == 13 # return key
evt.preventDefault()
return cbox.addClass "has-error" if input.val().length > 160 || input.val().trim().length == 0
input.attr 'disabled', 'disabled'
$.ajax
url: '/ajax/create_comment'
type: 'POST'
data:
answer: aid
comment: input.val()
dataType: 'json' # jQuery can't guess the datatype correctly here...
success: (data, status, jqxhr) ->
console.log data
if data.success
$(".panel-footer#ab-comments-#{aid}").html data.render
input.val ''
ctr.html 160
$("span#ab-comment-count-#{aid}").html data.count
showNotification data.message, data.success
error: (jqxhr, status, error) ->
console.log jqxhr, status, error
showNotification "An error occurred, a developer should check the console for details", false
complete: (jqxhr, status) ->
input.removeAttr 'disabled'
$(document).on "input", "input[name=ab-comment-new]", (evt) ->
input = $(this)
aid = input[0].dataset.aId
ctr = $("span#ab-comment-charcount-#{aid}")
cbox = $("div[name=ab-comment-new-group][data-a-id=#{aid}]")
cbox.removeClass "has-error" if cbox.hasClass "has-error"
ctr.html 160 - input.val().length
if Number(ctr.html()) < 0
ctr.removeClass 'text-muted'
ctr.addClass 'text-danger'
else
ctr.removeClass 'text-danger'
ctr.addClass 'text-muted'
$(document).on "click", "button[name=user-action]", ->
btn = $(this)

View File

@ -18,5 +18,6 @@ class Ajax::CommentController < ApplicationController
@message = "Comment posted successfully."
@success = true
@render = render_to_string(partial: 'shared/comments', locals: { a: answer })
@count = answer.comment_count
end
end

View File

@ -1,2 +1,3 @@
json.partial! 'ajax/shared/status'
json.render @render if @render
json.render @render if @render
json.count @count if @count

View File

@ -7,6 +7,6 @@
#{user_screen_name comment.user}:
= comment.content
- if user_signed_in?
.form-group.has-feedback
%input.form-control{type: :text, placeholder: 'Comment...', id: "ab-comments-#{a.id}-new"}
%span.text-muted.form-control-feedback.comment-count{id: "ab-comments-#{a.id}-charcount"} 160
.form-group.has-feedback{name: 'ab-comment-new-group', 'data-a-id' => a.id}
%input.form-control{type: :text, placeholder: 'Comment...', name: 'ab-comment-new', 'data-a-id' => a.id}
%span.text-muted.form-control-feedback.comment-count{id: "ab-comment-charcount-#{a.id}"} 160