DH DG; create comments via backbone
This commit is contained in:
parent
3f7d992a0b
commit
1ecfa92c9e
10 changed files with 80 additions and 47 deletions
|
|
@ -26,7 +26,7 @@ class CommentsController < ApplicationController
|
||||||
Postzord::Dispatcher.build(current_user, @comment).post
|
Postzord::Dispatcher.build(current_user, @comment).post
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js{ render(:create, :status => 201)}
|
format.json { render :json => @comment.as_api_response(:backbone) }
|
||||||
format.html{ render :nothing => true, :status => 201 }
|
format.html{ render :nothing => true, :status => 201 }
|
||||||
format.mobile{ render :partial => 'comment', :locals => {:post => @comment.post, :comment => @comment} }
|
format.mobile{ render :partial => 'comment', :locals => {:post => @comment.post, :comment => @comment} }
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
var post = App.stream.collection.get(<%= @comment.post.id %>);
|
App.stream.collection.get(<%= @comment.post.id %>);
|
||||||
|
|
||||||
console.log(post);
|
console.log(post);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,9 @@
|
||||||
%script{:id => "comment-template", :type => 'text/template'}
|
%script{:id => "comment-template", :type => 'text/template'}
|
||||||
!= File.read("#{Rails.root}/app/views/shared/_comment.html.underscore")
|
!= File.read("#{Rails.root}/app/views/shared/_comment.html.underscore")
|
||||||
|
|
||||||
|
%script{:id => "comment-stream-template", :type => 'text/template'}
|
||||||
|
!= File.read("#{Rails.root}/app/views/shared/_comment_stream.html.underscore")
|
||||||
|
|
||||||
.container{:style=> "#{yield(:break_the_mold)}"}
|
.container{:style=> "#{yield(:break_the_mold)}"}
|
||||||
- if @aspsect == :getting_started || @page == :logged_out
|
- if @aspsect == :getting_started || @page == :logged_out
|
||||||
= yield
|
= yield
|
||||||
|
|
|
||||||
27
app/views/shared/_comment_stream.html.underscore
Normal file
27
app/views/shared/_comment_stream.html.underscore
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<ul class="comments"> </ul>
|
||||||
|
|
||||||
|
<div class="new_comment_form_wrapper <%= comments_count > 0 ? '' : 'hidden' %>">
|
||||||
|
<form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" id="new_comment_on_<%= id %>" method="post">
|
||||||
|
<a href="/people/<%= current_user.id %>">
|
||||||
|
<img src="<%= current_user.avatar.small %>" class="avatar" data-person-id="<%= current_user.id %>"/>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<label for="comment_text_on_<%= id %>">Comment</label>
|
||||||
|
<textarea class="comment_box" id="comment_text_on_<%= id %>" name="text" rows="2" />
|
||||||
|
</p>
|
||||||
|
<div class="submit_button">
|
||||||
|
<input class="comment_submit button creation" id="comment_submit_<%= id %>" name="commit" type="submit" value="Comment" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
@ -67,37 +67,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="comments">
|
<div class="comments"> </div>
|
||||||
<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>
|
|
||||||
|
|
||||||
<ul class="comments"> </ul>
|
|
||||||
|
|
||||||
<div class="new_comment_form_wrapper <%= comments_count > 0 ? '' : 'hidden' %>">
|
|
||||||
<form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" data-remote="true" id="new_comment_on_<%= id %>" method="post">
|
|
||||||
<a href="/people/<%= current_user.id %>">
|
|
||||||
<img src="<%= current_user.avatar.small %>" class="avatar" data-person-id="<%= current_user.id %>"/>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<label for="comment_text_on_<%= id %>">Comment</label>
|
|
||||||
<textarea class="comment_box" id="comment_text_on_<%= id %>" name="text" rows="2" />
|
|
||||||
</p>
|
|
||||||
<div class="submit_button">
|
|
||||||
<input class="comment_submit button creation" data-disable-with="Commenting..." id="comment_submit_<%= id %>" name="commit" type="submit" value="Comment" />
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,5 @@
|
||||||
App.Models.Comment = Backbone.Model.extend({
|
App.Models.Comment = Backbone.Model.extend({
|
||||||
|
url: function() {
|
||||||
|
return "/posts/" + this.get("post_id") + "/comments";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
App.Models.Post = Backbone.Model.extend({
|
App.Models.Post = Backbone.Model.extend({
|
||||||
url: "/posts/:id",
|
url: "/posts",
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.comments = new App.Collections.Comments(this.get("last_three_comments"));
|
this.comments = new App.Collections.Comments(this.get("last_three_comments"));
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,45 @@
|
||||||
App.Views.CommentStream = Backbone.View.extend({
|
App.Views.CommentStream = Backbone.View.extend({
|
||||||
|
events: {
|
||||||
|
"submit form": "createComment"
|
||||||
|
},
|
||||||
|
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.collection = options.collection;
|
this.model = options.model;
|
||||||
|
this.template = _.template($("#comment-stream-template").html());
|
||||||
|
|
||||||
|
_.bindAll(this, "appendComment");
|
||||||
|
this.model.comments.bind("add", this.appendComment);
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.collection.each(function(comment) {
|
$(this.el).html(this.template($.extend(
|
||||||
$(self.el).append(new App.Views.Comment({
|
this.model.toJSON(),
|
||||||
model: comment
|
App.user()
|
||||||
}).render());
|
)));
|
||||||
});
|
|
||||||
|
this.model.comments.each(this.appendComment);
|
||||||
|
|
||||||
return this.el;
|
return this.el;
|
||||||
|
},
|
||||||
|
|
||||||
|
createComment: function(evt) {
|
||||||
|
evt.preventDefault();
|
||||||
|
|
||||||
|
this.model.comments.create({
|
||||||
|
"text" : this.$(".comment_box").val(),
|
||||||
|
"post_id" : this.model.id
|
||||||
|
});
|
||||||
|
|
||||||
|
this.$(".comment_box").val("");
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
appendComment: function(comment) {
|
||||||
|
this.$("ul.comments").append(new App.Views.Comment({
|
||||||
|
model: comment
|
||||||
|
}).render());
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@ App.Views.Comment = Backbone.View.extend({
|
||||||
App.user()
|
App.user()
|
||||||
)));
|
)));
|
||||||
|
|
||||||
console.log(this.el);
|
|
||||||
|
|
||||||
return this.el;
|
return this.el;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,26 @@
|
||||||
App.Views.Post = Backbone.View.extend({
|
App.Views.Post = Backbone.View.extend({
|
||||||
|
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.model = options.model;
|
this.model = options.model;
|
||||||
this.template = _.template($("#stream-element-template").html());
|
this.template = _.template($("#stream-element-template").html());
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
|
var self = this;
|
||||||
this.el = $(this.template($.extend(
|
this.el = $(this.template($.extend(
|
||||||
this.model.toJSON(),
|
this.model.toJSON(),
|
||||||
App.user()
|
App.user()
|
||||||
)));
|
)))[0];
|
||||||
|
|
||||||
this.$("ul.comments").html(new App.Views.CommentStream({
|
this.$(".comments").html(new App.Views.CommentStream({
|
||||||
collection: this.model.comments
|
model: this.model
|
||||||
}).render());
|
}).render());
|
||||||
|
|
||||||
this.$(".details time").timeago();
|
this.$(".details time").timeago();
|
||||||
this.$("label").inFieldLabels();
|
this.$("label").inFieldLabels();
|
||||||
|
|
||||||
|
Diaspora.BaseWidget.instantiate("StreamElement", $(this.el));
|
||||||
|
|
||||||
return this.el;
|
return this.el;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue