expand comments working

This commit is contained in:
danielgrippi 2011-12-16 10:40:19 -08:00 committed by Dennis Collinson
parent 574eb7b12d
commit 6911ed5d7d
4 changed files with 38 additions and 22 deletions

View file

@ -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

View file

@ -1,11 +1,14 @@
<div class="comment_stream">
<ul class="show_comments <%= comments_count <= 3 ? 'hidden' : '' %>">
<li>
<a href="/posts/<%= id %>/comments" class="toggle_post_comments">
Show <%= comments_count - 3 %> more comments
</a>
</li>
</ul>
<% if(typeof(all_comments_loaded) == 'undefined' || !all_comments_loaded) { %>
<ul class="show_comments <%= comments_count <= 3 ? 'hidden' : '' %>">
<li>
<a href="/posts/<%= id %>/comments" class="toggle_post_comments">
Show <%= comments_count - 3 %> more comments
</a>
</li>
</ul>
<% } %>
<ul class="comments"> </ul>

View file

@ -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(){

View file

@ -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();
}
});
}
});