Some refactoring, and make possible to remove own comments

openid
Micha Wrobel 13 years ago
parent 9a190cf474
commit e7f5f57424

@ -1,5 +1,8 @@
class AsciiIo.Models.Comment extends Backbone.Model class AsciiIo.Models.Comment extends Backbone.Model
user: ->
this.get('user')
class AsciiIo.Collections.Comments extends Backbone.Collection class AsciiIo.Collections.Comments extends Backbone.Collection
model: AsciiIo.Models.Comment model: AsciiIo.Models.Comment

@ -10,4 +10,8 @@
<div class="body">{{body}}</div> <div class="body">{{body}}</div>
</div> </div>
{{#if show_remove_link}}
<a href="#" class="remove">remove</a>
{{/if}}
<div class="clear"></div> <div class="clear"></div>

@ -0,0 +1,4 @@
class AsciiIo.Views.Base extends Backbone.View
current_user: ->
AsciiIo.current_user

@ -1,10 +1,28 @@
class AsciiIo.Views.CommentEntry extends Backbone.View class AsciiIo.Views.CommentEntry extends AsciiIo.Views.Base
template: JST['backbone/templates/comments/show'] template: JST['backbone/templates/comments/show']
tagName: 'li' tagName: 'li'
className: 'comment' className: 'comment'
render: -> events:
$(@el).html(@template(@model.toJSON())) 'click .remove': 'removeComment'
initialize:(options) ->
@collection = options.collection
render: ->
context = _.extend(@model.toJSON(), { show_remove_link: @showRemoveLink() })
$(@el).html @template(context)
this this
showRemoveLink: ->
@current_user() && ( @current_user().id == @model.user().id )
removeComment: (event) ->
event.preventDefault()
@model.destroy
wait: true
success: =>
$(this.el).slideUp("slow", =>
this.remove()
)

@ -1,4 +1,4 @@
class AsciiIo.Views.CommentsIndex extends Backbone.View class AsciiIo.Views.CommentsIndex extends AsciiIo.Views.Base
el: '#comments' el: '#comments'
template: JST['backbone/templates/comments/index'] template: JST['backbone/templates/comments/index']
@ -10,14 +10,12 @@ class AsciiIo.Views.CommentsIndex extends Backbone.View
@collection.on('reset', @render, this) @collection.on('reset', @render, this)
@collection.on('add', @render, this) @collection.on('add', @render, this)
@current_user = AsciiIo.current_user
render: -> render: ->
$(@el).html @template( show_form: @current_user ) $(@el).html @template( show_form: @current_user )
$comments = this.$('.comments') $comments = this.$('.comments')
@collection.each (comment) -> @collection.each (comment) =>
view = new AsciiIo.Views.CommentEntry({ model: comment, collection: @collection}) view = new AsciiIo.Views.CommentEntry({ model: comment, collection: @collection})
$comments.append view.render().el $comments.append view.render().el

@ -17,7 +17,7 @@ class Comment < ActiveRecord::Base
super({ super({
:include => { :include => {
:user => { :user => {
:only => [ :nickname, :avatar_url ] :only => [ :id, :nickname, :avatar_url ]
} }
}, },
:methods => [:created] :methods => [:created]

@ -26,7 +26,7 @@
var comments = new AsciiIo.Collections.Comments(); var comments = new AsciiIo.Collections.Comments();
var asciicast_id = <%= @asciicast.id %> var asciicast_id = <%= @asciicast.id %>
comments.url = ['/api/asciicasts/', asciicast_id, '/comments.json'].join(''); comments.url = ['/api/asciicasts/', asciicast_id, '/comments'].join('');
comments.fetch(); comments.fetch();
view = new AsciiIo.Views.CommentsIndex({ collection: comments }); view = new AsciiIo.Views.CommentsIndex({ collection: comments });

@ -1,5 +1,5 @@
<script type="text/javascript"> <script type="text/javascript">
jQuery(document).ready(function(){ jQuery(document).ready(function(){
AsciiIo.current_user = <%= raw current_user.to_json(:only => [:nickname, :avatar_url]) %>; AsciiIo.current_user = <%= raw current_user.to_json(:only => [:id, :nickname, :avatar_url]) %>;
}); });
</script> </script>

@ -14,16 +14,12 @@ describe Comment do
comment.user = user comment.user = user
end end
it "should include user.gravatar_url" do it "should include user comment creator properties user" do
hash = comment.as_json
hash.should include(:user)
hash[:user].should include("avatar_url")
end
it "should include user.nickname" do
hash = comment.as_json hash = comment.as_json
hash.should include(:user) hash.should include(:user)
hash[:user].should include("nickname") hash[:user].should include("nickname")
hash[:user].should include("avatar_url")
hash[:user].should include("id")
end end
it "should include comment.created" do it "should include comment.created" do

Loading…
Cancel
Save