diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index 29447ec2b..2ea5700ee 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -4,16 +4,5 @@ App.Views.Comment = App.Views.StreamObject.extend({ events : { "click .comment_delete": "destroyModel" - }, - - render: function() { - this.el = $(this.template($.extend( - this.model.toJSON(), - App.user() - ))); - - this.delegateEvents(); //we need this because we are explicitly setting this.el in this.render() - - return this; } }); diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index dd58bce84..4a41071f8 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -13,17 +13,12 @@ App.Views.Post = App.Views.StreamObject.extend({ }, render: function() { - this.el = $(this.template($.extend( - this.model.toJSON(), - App.user() - )))[0]; - - this.initializeTooltips() + this.renderTemplate() + .initializeTooltips() .renderPostContent() .renderComments(); this.$(".details time").timeago(); - this.delegateEvents(); //we need this because we are explicitly setting this.el in this.render() return this; }, @@ -75,7 +70,7 @@ App.Views.Post = App.Views.StreamObject.extend({ this.model.likes.get(link.data("id")).destroy(); } - return this; + return this }, expandLikes: function(evt){ diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js index 5ffad02c8..270f895db 100644 --- a/public/javascripts/app/views/stream_object_view.js +++ b/public/javascripts/app/views/stream_object_view.js @@ -1,8 +1,6 @@ App.Views.StreamObject = Backbone.View.extend({ initialize: function(options) { this.model = options.model; - this.template = _.template($(this.template_name).html()); - this.model.bind('remove', this.remove, this); }, @@ -13,5 +11,20 @@ App.Views.StreamObject = Backbone.View.extend({ remove: function() { $(this.el).remove(); + }, + + context : function(){ + var modelJson = this.model ? this.model.toJSON() : {} + return $.extend(modelJson, App.user()); + }, + + renderTemplate : function(){ + this.template = _.template($(this.template_name).html()); + $(this.el).html(this.template(this.context())); + return this; + }, + + render : function() { + return this.renderTemplate() } }); diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index c56cbf212..bdfae07c4 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -34,9 +34,9 @@ describe("App.views.Post", function(){ context("Reshare link", function(){ it("is present if the post is public", function(){ + var view = new App.Views.Post({model : this.statusMessage}).render(); this.statusMessage.set({"public" : true}); - var view = new App.Views.Post({model : this.statusMessage}).render(); var statusElement = $(view.el) expect(statusElement.find(".reshare_action")).toNotBe(null); @@ -53,22 +53,28 @@ describe("App.views.Post", function(){ }) context("Like link", function(){ - it("displays 'Unlike' if the current user has already liked the post", function(){ - this.statusMessage.set({user_like : null}); - - var view = new App.Views.Post({model : this.statusMessage}).render(); - var statusElement = $(view.el) - - expect(statusElement.find(".like_action a").text()).toContain('Like'); + beforeEach(function(){ + this.view = new App.Views.Post({model : this.statusMessage}) }) - it("displays 'Like' if the current user has not already liked the post", function(){ + it("clicking 'Like' toggles appropriately", function(){ + this.statusMessage.set({user_like : null}); + this.view.render() + var link = this.view.$(".like_action"); + + expect(link.text()).toContain('Like'); + link.click(); + expect(link.text()).toContain('Unlike'); + }) + + it("clicking 'UnLike' toggles appropriately", function(){ this.statusMessage.set({user_like : { id : 1 }}); + this.view.render() + var link = this.view.$(".like_action"); - var view = new App.Views.Post({model : this.statusMessage}).render(); - var statusElement = $(view.el) - - expect(statusElement.find(".like_action a").text()).toContain('Unlike'); + expect(link.text()).toContain('Unlike'); + link.click(); + expect(link.text()).toContain('Like'); }) }) }) diff --git a/spec/javascripts/app/views/stream_view_spec.js b/spec/javascripts/app/views/stream_view_spec.js index b1fcda671..f25a7dd4b 100644 --- a/spec/javascripts/app/views/stream_view_spec.js +++ b/spec/javascripts/app/views/stream_view_spec.js @@ -20,7 +20,7 @@ describe("App.views.Stream", function(){ context("when rendering a Status Mesasage", function(){ it("shows the status message in the content area", function(){ - expect(this.statusElement.find(".post-content p").text()).toContain("hella infos yo!") + expect(this.statusElement.find(".post-content p").text()).toContain("jimmy's 2 whales") }) }) })