Skip to content

Instantly share code, notes, and snippets.

@bhserna
Created November 9, 2012 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhserna/4047542 to your computer and use it in GitHub Desktop.
Save bhserna/4047542 to your computer and use it in GitHub Desktop.
#= require "commentator_templates"
#= require "commentator_poster"
#= require "commentator_params"
class window.Commentator
constructor: (args) ->
params = new CommentatorParams(args)
@el = params.el()
@url = params.url()
@poster = params.poster()
@comments = params.comments()
@reply_link_name = params.reply_link_name()
@display_form = params.display_form()
@comment_template = params.comment_template()
@reply_template = params.reply_template()
@comments_form_template = params.comments_form_template()
@replies_form_template = params.replies_form_template()
@on_comment = params.on_comment()
@on_reply = params.on_reply()
@el.delegate ".comments_form", "submit", @add_comment
@render_comments()
@render_form()
render_comments: ->
@comments_view = new Commentator.CommentsView(this, @comments)
@el.append @comments_view.render()
render_form: ->
if @display_form and @url?
@form_view = new Commentator.CommentFormView(this)
@el.append @form_view.render()
add_comment: (e) =>
e.preventDefault()
if @form_view.is_comment_valid()
@_save_comment()
_save_comment: ()->
data =
message: @form_view.comment()
@poster.post @url, data, (json) =>
@comments.push json
@comments_view.add_comment(json)
@form_view.clean()
class window.Commentator
constructor: (args) ->
params = new CommentatorParams(args)
#@el = params.el()
#@url = params.url()
#@poster = params.poster()
#@comments = params.comments()
#@reply_link_name = params.reply_link_name()
#@display_form = params.display_form()
#@comment_template = params.comment_template()
#@reply_template = params.reply_template()
#@comments_form_template = params.comments_form_template()
#@replies_form_template = params.replies_form_template()
#@on_comment_render = params.on_comment_render()
#@on_reply_render = params.on_reply_render()
# All this lines are no more needed =)
for param in params.all_params
@[param] = params[param]()
@el.delegate ".comments_form", "submit", @add_comment
@render_comments()
@render_form()
render_comments: ->
@comments_view = new Commentator.CommentsView(this, @comments)
@el.append @comments_view.render()
# .... the same code ....
class window.CommentatorParams
constructor: (@args) ->
el: ->
@args.el
url: ->
@args.url
poster: ->
@args.poster || new CommentatorPoster
comments: ->
@args.comments || @el.data "comments"
reply_link_name: ->
@args.reply_link_name || "Comment"
display_form: ->
if @args.display_form? then @args.display_form else true
comment_template: ->
@args.comment_template || CommentatorTemplates.comment
reply_template: ->
@args.reply_template || CommentatorTemplates.reply
comments_form_template: ->
@args.comments_form_template || CommentatorTemplates.comments_form
replies_form_template: ->
@args.replies_form_template || CommentatorTemplates.replies_form
on_comment: ->
@args.on_comment || @_no_op
on_reply: ->
@args.on_comment || @_no_op
_no_op: ->
class window.CommentatorParams
constructor: (@args) ->
all_params: ["el", "url", "poster", "comments", "reply_link_name", "display_form",
"comment_template", "reply_template", "comments_form_template",
"replies_form_template", "on_comment_render", "on_reply_render"]
el: ->
@args.el
url: ->
@args.url
# ..... the same code ......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment