pulling more view logic into superclass

This commit is contained in:
Dennis Collinson 2011-12-15 20:45:03 -08:00
parent b94b5240a8
commit a7627fabea
5 changed files with 38 additions and 35 deletions

View file

@ -4,16 +4,5 @@ App.Views.Comment = App.Views.StreamObject.extend({
events : { events : {
"click .comment_delete": "destroyModel" "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;
} }
}); });

View file

@ -13,17 +13,12 @@ App.Views.Post = App.Views.StreamObject.extend({
}, },
render: function() { render: function() {
this.el = $(this.template($.extend( this.renderTemplate()
this.model.toJSON(), .initializeTooltips()
App.user()
)))[0];
this.initializeTooltips()
.renderPostContent() .renderPostContent()
.renderComments(); .renderComments();
this.$(".details time").timeago(); this.$(".details time").timeago();
this.delegateEvents(); //we need this because we are explicitly setting this.el in this.render()
return this; return this;
}, },
@ -75,7 +70,7 @@ App.Views.Post = App.Views.StreamObject.extend({
this.model.likes.get(link.data("id")).destroy(); this.model.likes.get(link.data("id")).destroy();
} }
return this; return this
}, },
expandLikes: function(evt){ expandLikes: function(evt){

View file

@ -1,8 +1,6 @@
App.Views.StreamObject = Backbone.View.extend({ App.Views.StreamObject = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
this.model = options.model; this.model = options.model;
this.template = _.template($(this.template_name).html());
this.model.bind('remove', this.remove, this); this.model.bind('remove', this.remove, this);
}, },
@ -13,5 +11,20 @@ App.Views.StreamObject = Backbone.View.extend({
remove: function() { remove: function() {
$(this.el).remove(); $(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()
} }
}); });

View file

@ -34,9 +34,9 @@ describe("App.views.Post", function(){
context("Reshare link", function(){ context("Reshare link", function(){
it("is present if the post is public", 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}); this.statusMessage.set({"public" : true});
var view = new App.Views.Post({model : this.statusMessage}).render();
var statusElement = $(view.el) var statusElement = $(view.el)
expect(statusElement.find(".reshare_action")).toNotBe(null); expect(statusElement.find(".reshare_action")).toNotBe(null);
@ -53,22 +53,28 @@ describe("App.views.Post", function(){
}) })
context("Like link", function(){ context("Like link", function(){
it("displays 'Unlike' if the current user has already liked the post", function(){ beforeEach(function(){
this.statusMessage.set({user_like : null}); this.view = new App.Views.Post({model : this.statusMessage})
var view = new App.Views.Post({model : this.statusMessage}).render();
var statusElement = $(view.el)
expect(statusElement.find(".like_action a").text()).toContain('Like');
}) })
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.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(); expect(link.text()).toContain('Unlike');
var statusElement = $(view.el) link.click();
expect(link.text()).toContain('Like');
expect(statusElement.find(".like_action a").text()).toContain('Unlike');
}) })
}) })
}) })

View file

@ -20,7 +20,7 @@ describe("App.views.Stream", function(){
context("when rendering a Status Mesasage", function(){ context("when rendering a Status Mesasage", function(){
it("shows the status message in the content area", 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")
}) })
}) })
}) })