diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 62aaccda9..be6330683 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -27,6 +27,7 @@ app.views.Post = app.views.StreamObject.extend({ $(this.el).attr("id", this.model.get("guid")); this.model.bind('remove', this.remove, this); + this.model.bind('destroy', this.destroy, this); //subviews this.commentStreamView = new app.views.CommentStream({ model : this.model}); @@ -116,5 +117,15 @@ app.views.Post = app.views.StreamObject.extend({ authorIsNotCurrentUser : function() { return this.model.get("author").id != (!!app.user() && app.user().id) + }, + + isOnShowPage : function() { + return (!this.model.collection) && (this.model.url() == document.location.pathname); + }, + + destroy : function() { + if (this.isOnShowPage()) { + document.location.replace(Backbone.history.options.root); + } } }); diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index 4ecb33569..9eb66d9a2 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -96,5 +96,28 @@ describe("app.views.Post", function(){ }); }) }) + + context("user views their own post", function(){ + beforeEach(function(){ + this.statusMessage.set({ author: { + id : app.user().id + }}); + this.view = new app.views.Post({model : this.statusMessage}).render(); + }) + + it("contains remove post", function(){ + expect(this.view.$(".remove_post")).toExist(); + }) + + it("destroys the view when they delete a their post from the show page", function(){ + spyOn(window, "confirm").andReturn(true); + + this.view.$(".remove_post").click(); + + expect(window.confirm).toHaveBeenCalled(); + expect(this.view).not.toExist(); + }) + }) + }) });