move timeago and tooltip logic to the base View

This commit is contained in:
danielgrippi 2012-01-06 14:05:35 -08:00 committed by Dennis Collinson
parent d2807b5202
commit fd6e3bae62
6 changed files with 30 additions and 33 deletions

View file

@ -9,7 +9,11 @@ app.views.Base = Backbone.View.extend({
}, },
render : function() { render : function() {
return this.renderTemplate().renderSubviews() this.renderTemplate()
this.renderSubviews()
this.renderPluginWidgets()
return this
}, },
renderTemplate : function(){ renderTemplate : function(){
@ -17,7 +21,6 @@ app.views.Base = Backbone.View.extend({
var presenter = _.isFunction(this.presenter) ? this.presenter() : this.presenter var presenter = _.isFunction(this.presenter) ? this.presenter() : this.presenter
$(this.el).html(this.template(presenter)); $(this.el).html(this.template(presenter));
this.postRenderTemplate(); this.postRenderTemplate();
return this;
}, },
postRenderTemplate : $.noop, //hella callbax yo postRenderTemplate : $.noop, //hella callbax yo
@ -31,7 +34,10 @@ app.views.Base = Backbone.View.extend({
view.delegateEvents(); view.delegateEvents();
} }
}) })
return this
}, },
renderPluginWidgets : function() {
this.$(this.tooltipSelector).twipsy();
this.$("time").timeago();
}
}) })

View file

@ -17,8 +17,6 @@ app.views.CommentStream = app.views.Base.extend({
postRenderTemplate : function() { postRenderTemplate : function() {
this.$("label").inFieldLabels(); this.$("label").inFieldLabels();
this.model.comments.each(this.appendComment, this); this.model.comments.each(this.appendComment, this);
return this;
}, },
createComment: function(evt) { createComment: function(evt) {

View file

@ -19,11 +19,7 @@ app.views.Post = app.views.StreamObject.extend({
".post-content" : "postContentView" ".post-content" : "postContentView"
}, },
tooltips : [ tooltipSelector : ".delete, .block_user, .post_scope",
".delete",
".block_user",
".post_scope"
],
initialize : function() { initialize : function() {
// set the guid // set the guid
@ -52,14 +48,6 @@ app.views.Post = app.views.StreamObject.extend({
return new postClass({ model : this.model }); return new postClass({ model : this.model });
}, },
postRenderTemplate : function() {
this.initializeTooltips();
this.$("time").timeago();
return this;
},
removeNsfwShield: function(evt){ removeNsfwShield: function(evt){
if(evt){ evt.preventDefault(); } if(evt){ evt.preventDefault(); }
@ -108,14 +96,6 @@ app.views.Post = app.views.StreamObject.extend({
this.$(".new_comment_form_wrapper").removeClass("hidden"); this.$(".new_comment_form_wrapper").removeClass("hidden");
this.$(".comment_box").focus(); this.$(".comment_box").focus();
return this;
},
initializeTooltips: function(){
_.each(this.tooltips, function(selector, options){
this.$(selector).twipsy(options);
}, this);
return this; return this;
} }
}); });

View file

@ -1,14 +1,10 @@
app.views.StreamObject = app.views.Base.extend({ app.views.StreamObject = app.views.Base.extend({
initialize: function(options) { initialize: function(options) {
this.model.bind('remove', this.remove, this); this.model.bind('remove', this.remove, this);
this.model.bind('change', this.render, this); this.model.bind('change', this.render, this);
}, },
postRenderTemplate : function(){ postRenderTemplate : function(){
// time
this.$("time").timeago();
// collapse long posts // collapse long posts
this.$(".collapsible").expander({ this.$(".collapsible").expander({
slicePoint: 400, slicePoint: 400,
@ -16,7 +12,6 @@ app.views.StreamObject = app.views.Base.extend({
expandText: Diaspora.I18n.t("show_more"), expandText: Diaspora.I18n.t("show_more"),
userCollapse: false userCollapse: false
}); });
}, },
destroyModel: function(evt){ destroyModel: function(evt){

View file

@ -84,7 +84,7 @@
// temp hack to check if backbone is enabled for the page // temp hack to check if backbone is enabled for the page
Diaspora.backboneEnabled = function(){ Diaspora.backboneEnabled = function(){
return window.app.stream !== undefined; return window.app && window.app.stream !== undefined;
} }
window.Diaspora = Diaspora; window.Diaspora = Diaspora;

View file

@ -71,5 +71,23 @@ describe("app.views.Base", function(){
expect(this.view.$('.subview2').text().trim()).toBe("furreal this is the Second Subview") 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")
})
})
}) })
}) })