From f5172fa977c0747b652b1688333088113bdb48f8 Mon Sep 17 00:00:00 2001 From: danielgrippi <danielgrippi@gmail.com> Date: Tue, 13 Dec 2011 18:12:04 -0800 Subject: [PATCH] comment and post deletion now works on the backbone stream --- app/controllers/comments_controller.rb | 2 + app/controllers/posts_controller.rb | 1 + app/views/templates/reshare.ujs | 52 +++++++++++--------- app/views/templates/stream_element.ujs | 2 +- public/javascripts/app/models/comment.js | 4 +- public/javascripts/app/models/post.js | 4 +- public/javascripts/app/views/comment_view.js | 19 +++++++ public/javascripts/app/views/post_view.js | 16 +++++- 8 files changed, 72 insertions(+), 28 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 4392952506..2305a2ee59 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -44,12 +44,14 @@ class CommentsController < ApplicationController current_user.retract(@comment) respond_to do |format| format.js { render :nothing => true, :status => 204 } + format.json { render :nothing => true, :status => 204 } format.mobile{ redirect_to @comment.post } end else respond_to do |format| format.mobile {redirect_to :back} format.js {render :nothing => true, :status => 403} + format.json { render :nothing => true, :status => 403 } end end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9a3210d49f..4af8f7e9f9 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -52,6 +52,7 @@ class PostsController < ApplicationController current_user.retract(@post) respond_to do |format| format.js {render 'destroy'} + format.json { render :nothing => true, :status => 204 } format.all {redirect_to multi_path} end else diff --git a/app/views/templates/reshare.ujs b/app/views/templates/reshare.ujs index 480df78da2..04308a946d 100644 --- a/app/views/templates/reshare.ujs +++ b/app/views/templates/reshare.ujs @@ -1,28 +1,36 @@ <div class="reshare"> - <a href="/people/<%= root.author.id %>"> - <img src="<%= root.author.avatar.small %>" class="avatar" data-person-id="<%= root.author.id %>"/> - </a> + <% if(root) { %> - <div class="content"> - <div class="post_initial_info"> - <span class="from"> - <a href="/people/<%= root.author.id %>"> - <%= root.author.name %> - </a> - </span> - <span class="details"> - - - <a href="/posts/<%= root.id %>"> - <time class="timeago" datetime="<%= root.created_at %>"/> - </a> - </span> - </div> + <a href="/people/<%= root.author.id %>"> + <img src="<%= root.author.avatar.small %>" class="avatar" data-person-id="<%= root.author.id %>"/> + </a> + + <div class="content"> + <div class="post_initial_info"> + <span class="from"> + <a href="/people/<%= root.author.id %>"> + <%= root.author.name %> + </a> + </span> + <span class="details"> + - + <a href="/posts/<%= root.id %>"> + <time class="timeago" datetime="<%= root.created_at %>"/> + </a> + </span> + </div> - <!-- duplicate from statusmessage partial --> - <p> - <%= root.text %> - </p> + <!-- duplicate from statusmessage partial --> + <p> + <%= root.text %> + </p> + + </div> - </div> + <% } else { %> + <p> + Original post deleted by author. + </p> + <% } %> </div> diff --git a/app/views/templates/stream_element.ujs b/app/views/templates/stream_element.ujs index a8f057de8b..48cece6180 100644 --- a/app/views/templates/stream_element.ujs +++ b/app/views/templates/stream_element.ujs @@ -75,7 +75,7 @@ </span> · - <% if(public) { %> + <% if(public && author.id != current_user.id) { %> <span class="reshare_action"> <% if(root) { var rootGuid = root.guid; diff --git a/public/javascripts/app/models/comment.js b/public/javascripts/app/models/comment.js index 4c2f3fe95b..6ce1fe181f 100644 --- a/public/javascripts/app/models/comment.js +++ b/public/javascripts/app/models/comment.js @@ -1,5 +1,3 @@ App.Models.Comment = Backbone.Model.extend({ - url: function() { - return "/posts/" + this.get("post_id") + "/comments"; - } + urlRoot: "/comments" }); diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index 806af2e979..0e7fa67bc0 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -1,5 +1,7 @@ App.Models.Post = Backbone.Model.extend({ - url: "/posts", + url: function(){ + return "/posts/" + this.get("id"); + }, initialize: function() { this.comments = new App.Collections.Comments(this.get("last_three_comments")); diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index 1e91da727b..aeafc1670c 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -1,4 +1,8 @@ App.Views.Comment = Backbone.View.extend({ + events : { + "click .delete": "destroyComment" + }, + initialize: function(options) { this.model = options.model; this.template = _.template($("#comment-template").html()); @@ -10,6 +14,21 @@ App.Views.Comment = Backbone.View.extend({ App.user() ))); + this.delegateEvents(); //we need this because we are explicitly setting this.el in this.render() + return this; + }, + + // NOTE: pull this out into a base class + destroyComment: function(evt) { + if(evt) { evt.preventDefault() } + + var domElement = this.el; + + this.model.destroy({ + success: function(){ + $(domElement).remove(); + } + }); } }); diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index a2773215f0..0e30b5e39e 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -2,7 +2,8 @@ App.Views.Post = Backbone.View.extend({ events: { "click .focus_comment_textarea": "focusCommentTextarea", - "focus .comment_box": "commentTextareaFocused" + "focus .comment_box": "commentTextareaFocused", + "click .delete:first": "destroyPost" }, initialize: function(options) { @@ -51,6 +52,19 @@ App.Views.Post = Backbone.View.extend({ commentTextareaFocused: function(evt){ this.$("form").removeClass('hidden').addClass("open"); + }, + + // NOTE: pull this out into a base class + destroyPost: function(evt){ + if(evt){ evt.preventDefault(); } + + var domElement = this.el; + + this.model.destroy({ + success: function(){ + $(domElement).remove(); + } + }); } }); -- GitLab