From 79208e90731af9ce50fd5c343a10f15965890ed3 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Mon, 26 Dec 2011 00:55:02 -0500 Subject: [PATCH] CommentStream inherits from Base view; using subviews in Post view; be a little smarter about bindAll in Stream view --- .../app/views/commment_stream_view.js | 21 ++++------ public/javascripts/app/views/post_view.js | 41 ++++++++----------- public/javascripts/app/views/stream_view.js | 11 +++-- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/public/javascripts/app/views/commment_stream_view.js b/public/javascripts/app/views/commment_stream_view.js index 7dbed9fc7..818e703fa 100644 --- a/public/javascripts/app/views/commment_stream_view.js +++ b/public/javascripts/app/views/commment_stream_view.js @@ -1,4 +1,7 @@ -App.Views.CommentStream = Backbone.View.extend({ +App.Views.CommentStream = App.Views.Base.extend({ + + template_name: "#comment-stream-template", + events: { "submit form": "createComment", "focus .comment_box": "commentTextareaFocused", @@ -6,28 +9,18 @@ App.Views.CommentStream = Backbone.View.extend({ }, initialize: function(options) { - this.model = options.model; - this.template = _.template($("#comment-stream-template").html()); - - _.bindAll(this, "appendComment"); this.model.comments.bind('add', this.appendComment, this); }, - render: function() { - $(this.el).html(this.template($.extend( - this.model.toJSON(), - App.user() - ))); - + postRenderTemplate : function() { this.$("label").inFieldLabels(); - - this.model.comments.each(this.appendComment); + this.model.comments.each(this.appendComment, this); return this; }, createComment: function(evt) { - evt.preventDefault(); + if(evt){ evt.preventDefault(); } this.model.comments.create({ "text" : this.$(".comment_box").val(), diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 3154197d6..31bf07474 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -10,16 +10,23 @@ App.Views.Post = App.Views.StreamObject.extend({ "click .block_user": "blockUser" }, - render: function() { - // subview rendering - this.renderTemplate() - .renderPostContent() - .renderFeedback() - .renderComments(); - // + subviews : { + ".feedback" : "feedbackView", + ".comments" : "commentStreamView" + }, - this.initializeTooltips(); - this.$(".details time").timeago(); + initialize : function() { + this.feedbackView = new App.Views.Feedback({model : this.model}); + this.commentStreamView = new App.Views.CommentStream({ model : this.model}); + + return this; + }, + + postRenderTemplate : function() { + this.renderPostContent() + .initializeTooltips() + .$(".details time") + .timeago(); return this; }, @@ -34,22 +41,6 @@ App.Views.Post = App.Views.StreamObject.extend({ return this; }, - renderComments: function(){ - this.$(".comments").html(new App.Views.CommentStream({ - model: this.model - }).render().el); - - return this; - }, - - renderFeedback: function(){ - this.$(".feedback").html(new App.Views.Feedback({ - model: this.model - }).render().el); - - return this; - }, - removeNsfwShield: function(evt){ if(evt){ evt.preventDefault(); } diff --git a/public/javascripts/app/views/stream_view.js b/public/javascripts/app/views/stream_view.js index c7542a4c6..afba1a179 100644 --- a/public/javascripts/app/views/stream_view.js +++ b/public/javascripts/app/views/stream_view.js @@ -4,20 +4,20 @@ App.Views.Stream = Backbone.View.extend({ }, initialize: function() { - _.bindAll(this, "appendPost", "collectionFetched", "loadMore"); + _.bindAll(this, "collectionFetched"); this.collection = this.collection || new App.Collections.Stream; - this.collection.bind("add", this.appendPost); + this.collection.bind("add", this.appendPost, this); }, render : function(){ - _.each(this.collection.models, this.appendPost) + _.each(this.collection.models, this.appendPost, this) return this; }, appendPost: function(post) { - var postView = new App.Views.Post({ model: post }).render(); - $(this.el).append(postView.el); + var postView = new App.Views.Post({ model: post }); + $(this.el).append(postView.render().el); }, collectionFetched: function() { @@ -32,7 +32,6 @@ App.Views.Stream = Backbone.View.extend({ if(evt) { evt.preventDefault(); } this.addLoader(); - this.collection.fetch({ add: true, success: this.collectionFetched