Skip to content

Instantly share code, notes, and snippets.

@sapslaj
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save sapslaj/8306750ff9d71e8a59ff to your computer and use it in GitHub Desktop.

Select an option

Save sapslaj/8306750ff9d71e8a59ff to your computer and use it in GitHub Desktop.
my take on MVC on the client
class ArticleViewController extends ViewController
initialize: (@params) ->
@event.on('button.new-comment', 'click', @new_comment)
@event.on('button.like', 'click', @like)
new_comment: (event) ->
comment_body = $('input.new-comment').val()
comment = new Comment({body: comment_body})
if comment.save()
flash_notice("Comment posted!")
else
flash_alert("There was an error posting your comment")
like: (event) ->
$like_container = $(".article-likes").
old_likes = parseInt($like_container.html())
article = Article.find(@params.id)
article.like()
likes = ++old_likes
$like_container.html(likes)
class Article extends Model
url: articles_path()
like: () ->
endpoint.vote({type: 'up'})
class Comment extends Model
url: article_comments_path(window.active_controller.id)
Router.draw ->
route articles_path(':id'), ArticleViewContoller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment