From 6911ed5d7dc76b1b609a3bb9dedc7a5860d3514b Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 16 Dec 2011 10:40:19 -0800 Subject: [PATCH] expand comments working --- app/controllers/comments_controller.rb | 15 +++++++-------- app/views/templates/comment_stream.ujs | 17 ++++++++++------- public/javascripts/app/models/post.js | 11 ++++++----- .../app/views/commment_stream_view.js | 17 +++++++++++++++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 2305a2ee5..16b5a249a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,8 +6,9 @@ class CommentsController < ApplicationController include ApplicationHelper before_filter :authenticate_user!, :except => [:index] - respond_to :html, :mobile, :except => :show - respond_to :js, :only => [:index] + respond_to :html, + :mobile, + :json rescue_from ActiveRecord::RecordNotFound do render :nothing => true, :status => 404 @@ -26,7 +27,7 @@ class CommentsController < ApplicationController Postzord::Dispatcher.build(current_user, @comment).post respond_to do |format| - format.json { render :json => @comment.as_api_response(:backbone), :status => 201 } + format.json{ render :json => @comment.as_api_response(:backbone), :status => 201 } format.html{ render :nothing => true, :status => 201 } format.mobile{ render :partial => 'comment', :locals => {:post => @comment.post, :comment => @comment} } end @@ -65,13 +66,11 @@ class CommentsController < ApplicationController if @post @comments = @post.comments.includes(:author => :profile).order('created_at ASC') - render :layout => false + respond_with do |format| + format.json { render :json => @post.comments.as_api_response(:backbone), :status => 200 } + end else raise ActiveRecord::RecordNotFound.new end end - - def new - render :layout => false - end end diff --git a/app/views/templates/comment_stream.ujs b/app/views/templates/comment_stream.ujs index 7204940a6..ab4158941 100644 --- a/app/views/templates/comment_stream.ujs +++ b/app/views/templates/comment_stream.ujs @@ -1,11 +1,14 @@
- + + <% if(typeof(all_comments_loaded) == 'undefined' || !all_comments_loaded) { %> + + <% } %> diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index 528fabbd1..fd3e8d3cc 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -1,13 +1,14 @@ App.Models.Post = Backbone.Model.extend({ - url: function(){ - return "/posts/" + this.id; - }, - initialize: function() { this.comments = new App.Collections.Comments(this.get("last_three_comments")); + this.comments.url = this.url() + '/comments'; this.likes = new App.Collections.Likes(this.get("user_like")); // load in the user like initially - this.likes.url = '/posts/' + this.id + '/likes'; + this.likes.url = this.url() + '/likes'; + }, + + url: function(){ + return "/posts/" + this.id; }, createdAt: function(){ diff --git a/public/javascripts/app/views/commment_stream_view.js b/public/javascripts/app/views/commment_stream_view.js index 345d8c1c7..7dbed9fc7 100644 --- a/public/javascripts/app/views/commment_stream_view.js +++ b/public/javascripts/app/views/commment_stream_view.js @@ -1,7 +1,8 @@ App.Views.CommentStream = Backbone.View.extend({ events: { "submit form": "createComment", - "focus .comment_box": "commentTextareaFocused" + "focus .comment_box": "commentTextareaFocused", + "click .toggle_post_comments": "expandComments" }, initialize: function(options) { @@ -9,7 +10,7 @@ App.Views.CommentStream = Backbone.View.extend({ this.template = _.template($("#comment-stream-template").html()); _.bindAll(this, "appendComment"); - this.model.comments.bind("add", this.appendComment); + this.model.comments.bind('add', this.appendComment, this); }, render: function() { @@ -45,6 +46,18 @@ App.Views.CommentStream = Backbone.View.extend({ commentTextareaFocused: function(evt){ this.$("form").removeClass('hidden').addClass("open"); + }, + + expandComments: function(evt){ + if(evt){ evt.preventDefault(); } + + var self = this; + this.model.comments.fetch({ + success : function(){ + self.model.set({all_comments_loaded : true}); + self.render(); + } + }); } });