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
user: ->
this.get('user')
class AsciiIo.Collections.Comments extends Backbone.Collection
model: AsciiIo.Models.Comment

@ -10,4 +10,8 @@
<div class="body">{{body}}</div>
</div>
{{#if show_remove_link}}
<a href="#" class="remove">remove</a>
{{/if}}
<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']
tagName: 'li'
className: 'comment'
render: ->
$(@el).html(@template(@model.toJSON()))
events:
'click .remove': 'removeComment'
initialize:(options) ->
@collection = options.collection
render: ->
context = _.extend(@model.toJSON(), { show_remove_link: @showRemoveLink() })
$(@el).html @template(context)
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'
template: JST['backbone/templates/comments/index']
@ -10,14 +10,12 @@ class AsciiIo.Views.CommentsIndex extends Backbone.View
@collection.on('reset', @render, this)
@collection.on('add', @render, this)
@current_user = AsciiIo.current_user
render: ->
$(@el).html @template( show_form: @current_user )
$comments = this.$('.comments')
@collection.each (comment) ->
@collection.each (comment) =>
view = new AsciiIo.Views.CommentEntry({ model: comment, collection: @collection})
$comments.append view.render().el

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

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

@ -1,5 +1,5 @@
<script type="text/javascript">
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>

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

Loading…
Cancel
Save