From 525fd3c4e46db1ac42e13fced0ceaf306ff50340 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 13 Dec 2011 19:47:26 -0800 Subject: [PATCH] make views associated in stream inherit from App.Views.StreamObject (location of shared deletion function) --- config/assets.yml | 1 + public/javascripts/app/views/comment_view.js | 25 ++++-------------- .../app/views/commment_stream_view.js | 2 ++ public/javascripts/app/views/post_view.js | 26 +++---------------- .../app/views/stream_object_view.js | 17 ++++++++++++ spec/javascripts/support/jasmine.yml | 1 + 6 files changed, 30 insertions(+), 42 deletions(-) create mode 100644 public/javascripts/app/views/stream_object_view.js diff --git a/config/assets.yml b/config/assets.yml index 284305160..6ac84d53e 100644 --- a/config/assets.yml +++ b/config/assets.yml @@ -19,6 +19,7 @@ javascripts: - public/javascripts/app/router.js - public/javascripts/app/models/* - public/javascripts/app/collections/* + - public/javascripts/app/views/stream_object_view.js - public/javascripts/app/views/* - public/javascripts/rails.validations.js diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index aeafc1670..d2f635f1d 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -1,11 +1,9 @@ -App.Views.Comment = Backbone.View.extend({ - events : { - "click .delete": "destroyComment" - }, +App.Views.Comment = App.Views.StreamObject.extend({ - initialize: function(options) { - this.model = options.model; - this.template = _.template($("#comment-template").html()); + template_name: "#comment-template", + + events : { + "click .delete:first": "destroyModel" }, render: function() { @@ -17,18 +15,5 @@ App.Views.Comment = Backbone.View.extend({ this.delegateEvents(); //we need this because we are explicitly setting this.el in this.render() return this; - }, - - // NOTE: pull this out into a base class - destroyComment: function(evt) { - if(evt) { evt.preventDefault() } - - var domElement = this.el; - - this.model.destroy({ - success: function(){ - $(domElement).remove(); - } - }); } }); diff --git a/public/javascripts/app/views/commment_stream_view.js b/public/javascripts/app/views/commment_stream_view.js index 9fc9b90f5..bfdc905f5 100644 --- a/public/javascripts/app/views/commment_stream_view.js +++ b/public/javascripts/app/views/commment_stream_view.js @@ -19,6 +19,8 @@ App.Views.CommentStream = Backbone.View.extend({ App.user() ))); + this.$("label").inFieldLabels(); + this.model.comments.each(this.appendComment); return this; diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 0e30b5e39..96844c885 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -1,14 +1,11 @@ -App.Views.Post = Backbone.View.extend({ +App.Views.Post = App.Views.StreamObject.extend({ + + template_name: "#stream-element-template", events: { "click .focus_comment_textarea": "focusCommentTextarea", "focus .comment_box": "commentTextareaFocused", - "click .delete:first": "destroyPost" - }, - - initialize: function(options) { - this.model = options.model; - this.template = _.template($("#stream-element-template").html()); + "click .delete:first": "destroyModel" }, render: function() { @@ -27,7 +24,6 @@ App.Views.Post = Backbone.View.extend({ this.renderPostContent(); this.$(".details time").timeago(); - this.$("label").inFieldLabels(); return this; }, @@ -52,19 +48,5 @@ App.Views.Post = Backbone.View.extend({ commentTextareaFocused: function(evt){ this.$("form").removeClass('hidden').addClass("open"); - }, - - // NOTE: pull this out into a base class - destroyPost: function(evt){ - if(evt){ evt.preventDefault(); } - - var domElement = this.el; - - this.model.destroy({ - success: function(){ - $(domElement).remove(); - } - }); } - }); diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js new file mode 100644 index 000000000..396c4503d --- /dev/null +++ b/public/javascripts/app/views/stream_object_view.js @@ -0,0 +1,17 @@ +App.Views.StreamObject = Backbone.View.extend({ + initialize: function(options) { + this.model = options.model; + this.template = _.template($(this.template_name).html()); + }, + + destroyModel: function(evt){ + if(evt){ evt.preventDefault(); } + + var domElement = this.el; + this.model.destroy({ + success: function(){ + $(domElement).remove(); + } + }); + } +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 3c9a97b5c..71531e9f8 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -40,6 +40,7 @@ src_files: - public/javascripts/app/router.js - public/javascripts/app/models/* - public/javascripts/app/collections/* + - public/javascripts/app/views/stream_object_view.js - public/javascripts/app/views/* - public/javascripts/mobile.js