From fd6e3bae62f01f7d0acf0154a936e242b6cecfcb Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 6 Jan 2012 14:05:35 -0800 Subject: [PATCH] move timeago and tooltip logic to the base View --- public/javascripts/app/views.js | 14 ++++++++---- .../app/views/commment_stream_view.js | 2 -- public/javascripts/app/views/post_view.js | 22 +------------------ .../app/views/stream_object_view.js | 5 ----- public/javascripts/diaspora.js | 2 +- spec/javascripts/app/views_spec.js | 18 +++++++++++++++ 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/public/javascripts/app/views.js b/public/javascripts/app/views.js index 65ba45f40..e3581607f 100644 --- a/public/javascripts/app/views.js +++ b/public/javascripts/app/views.js @@ -9,7 +9,11 @@ app.views.Base = Backbone.View.extend({ }, render : function() { - return this.renderTemplate().renderSubviews() + this.renderTemplate() + this.renderSubviews() + this.renderPluginWidgets() + + return this }, renderTemplate : function(){ @@ -17,7 +21,6 @@ app.views.Base = Backbone.View.extend({ var presenter = _.isFunction(this.presenter) ? this.presenter() : this.presenter $(this.el).html(this.template(presenter)); this.postRenderTemplate(); - return this; }, postRenderTemplate : $.noop, //hella callbax yo @@ -31,7 +34,10 @@ app.views.Base = Backbone.View.extend({ view.delegateEvents(); } }) - - return this }, + + renderPluginWidgets : function() { + this.$(this.tooltipSelector).twipsy(); + this.$("time").timeago(); + } }) diff --git a/public/javascripts/app/views/commment_stream_view.js b/public/javascripts/app/views/commment_stream_view.js index 94ea07ec0..5348ed55d 100644 --- a/public/javascripts/app/views/commment_stream_view.js +++ b/public/javascripts/app/views/commment_stream_view.js @@ -17,8 +17,6 @@ app.views.CommentStream = app.views.Base.extend({ postRenderTemplate : function() { this.$("label").inFieldLabels(); this.model.comments.each(this.appendComment, this); - - return this; }, createComment: function(evt) { diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 9468600d5..42b9379f5 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -19,11 +19,7 @@ app.views.Post = app.views.StreamObject.extend({ ".post-content" : "postContentView" }, - tooltips : [ - ".delete", - ".block_user", - ".post_scope" - ], + tooltipSelector : ".delete, .block_user, .post_scope", initialize : function() { // set the guid @@ -52,14 +48,6 @@ app.views.Post = app.views.StreamObject.extend({ return new postClass({ model : this.model }); }, - postRenderTemplate : function() { - this.initializeTooltips(); - this.$("time").timeago(); - - return this; - }, - - removeNsfwShield: function(evt){ if(evt){ evt.preventDefault(); } @@ -108,14 +96,6 @@ app.views.Post = app.views.StreamObject.extend({ this.$(".new_comment_form_wrapper").removeClass("hidden"); this.$(".comment_box").focus(); - return this; - }, - - initializeTooltips: function(){ - _.each(this.tooltips, function(selector, options){ - this.$(selector).twipsy(options); - }, this); - return this; } }); diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js index 253a9c654..917fc45d2 100644 --- a/public/javascripts/app/views/stream_object_view.js +++ b/public/javascripts/app/views/stream_object_view.js @@ -1,14 +1,10 @@ app.views.StreamObject = app.views.Base.extend({ - initialize: function(options) { this.model.bind('remove', this.remove, this); this.model.bind('change', this.render, this); }, postRenderTemplate : function(){ - // time - this.$("time").timeago(); - // collapse long posts this.$(".collapsible").expander({ slicePoint: 400, @@ -16,7 +12,6 @@ app.views.StreamObject = app.views.Base.extend({ expandText: Diaspora.I18n.t("show_more"), userCollapse: false }); - }, destroyModel: function(evt){ diff --git a/public/javascripts/diaspora.js b/public/javascripts/diaspora.js index e9fa97d89..d5ea266fb 100644 --- a/public/javascripts/diaspora.js +++ b/public/javascripts/diaspora.js @@ -84,7 +84,7 @@ // temp hack to check if backbone is enabled for the page Diaspora.backboneEnabled = function(){ - return window.app.stream !== undefined; + return window.app && window.app.stream !== undefined; } window.Diaspora = Diaspora; diff --git a/spec/javascripts/app/views_spec.js b/spec/javascripts/app/views_spec.js index 5ee20bcdb..2217796d0 100644 --- a/spec/javascripts/app/views_spec.js +++ b/spec/javascripts/app/views_spec.js @@ -71,5 +71,23 @@ describe("app.views.Base", function(){ expect(this.view.$('.subview2').text().trim()).toBe("furreal this is the Second Subview") }) }) + + context("calling out to third party plugins", function(){ + it("replaces .time with relative time ago in words", function(){ + spyOn($.fn, "timeago") + this.view.render() + expect($.fn.timeago).toHaveBeenCalled() + expect($.fn.timeago.mostRecentCall.object.selector).toBe("time") + }) + + + it("initializes tooltips declared with the view's tooltipSelector property", function(){ + this.view.tooltipSelector = ".christopher_columbus, .barrack_obama, .block_user" + + spyOn($.fn, "twipsy") + this.view.render() + expect($.fn.twipsy.mostRecentCall.object.selector).toBe(".christopher_columbus, .barrack_obama, .block_user") + }) + }) }) })