diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index 8a96169f6..f5d707bc4 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -15,6 +15,16 @@ app.models.Post = Backbone.Model.extend({ } }, + reshare : function(){ + var reshare = new app.models.Reshare(); + reshare.save({root_guid : this.baseGuid()}, { + success : function(){ + app.stream.collection.add(reshare.toJSON()); + } + }); + return reshare; + }, + toggleLike : function() { var userLike = this.get("user_like") if(userLike) { diff --git a/public/javascripts/app/models/reshare.js b/public/javascripts/app/models/reshare.js index 826274aba..a88f62538 100644 --- a/public/javascripts/app/models/reshare.js +++ b/public/javascripts/app/models/reshare.js @@ -1,3 +1,8 @@ app.models.Reshare = app.models.Post.extend({ - url : function() { return "/reshares"; } + url : function() { return "/reshares"; }, + + rootPost : function(){ + this._rootPost = this._rootPost || new app.models.Post(this.get("root")) + return this._rootPost + } }); diff --git a/public/javascripts/app/views.js b/public/javascripts/app/views.js index 5864606c6..4ff454ecb 100644 --- a/public/javascripts/app/views.js +++ b/public/javascripts/app/views.js @@ -3,6 +3,11 @@ app.views.Base = Backbone.View.extend({ return this.defaultPresenter() }, + setupRenderEvents : function(){ + this.model.bind('remove', this.remove, this); + this.model.bind('change', this.render, this); + }, + defaultPresenter : function(){ var modelJson = this.model ? this.model.toJSON() : {} return _.extend(modelJson, { current_user: app.user().current_user }); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index 363c5b77d..c45dbdbf8 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -13,16 +13,14 @@ app.views.Feedback = app.views.StreamObject.extend({ this.model.toggleLike(); }, + initialize: function(options){ + this.setupRenderEvents(); + this.reshareablePost = options.model; + }, + resharePost : function(evt){ if(evt) { evt.preventDefault(); } - if(!window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) { return } - - var reshare = new app.models.Reshare(); - reshare.save({root_guid : this.model.baseGuid()}, { - success : function(){ - app.stream.collection.add(reshare.toJSON()); - } - }); - return reshare; + if(!window.confirm("Reshare " + this.reshareablePost.baseAuthor().name + "'s post?")) { return } + this.reshareablePost.reshare(); } }) diff --git a/public/javascripts/app/views/reshare_feedback_view.js b/public/javascripts/app/views/reshare_feedback_view.js index 44744f9ed..3c89e5f2d 100644 --- a/public/javascripts/app/views/reshare_feedback_view.js +++ b/public/javascripts/app/views/reshare_feedback_view.js @@ -1,3 +1,6 @@ -app.views.ReshareFeedback = app.views.Base.extend({ - template_name: "#reshare-feedback-template", +app.views.ReshareFeedback = app.views.Feedback.extend({ + initialize : function(){ + this.reshareablePost = (this.model instanceof app.models.Reshare) ? this.model.rootPost() : new app.models.Reshare(this.model.attributes).rootPost(); + this.setupRenderEvents(); + } }); diff --git a/public/javascripts/app/views/stream_object_view.js b/public/javascripts/app/views/stream_object_view.js index 917fc45d2..5f14bd23e 100644 --- a/public/javascripts/app/views/stream_object_view.js +++ b/public/javascripts/app/views/stream_object_view.js @@ -1,7 +1,6 @@ app.views.StreamObject = app.views.Base.extend({ initialize: function(options) { - this.model.bind('remove', this.remove, this); - this.model.bind('change', this.render, this); + this.setupRenderEvents(); }, postRenderTemplate : function(){ diff --git a/spec/javascripts/app/models/reshare_spec.js b/spec/javascripts/app/models/reshare_spec.js new file mode 100644 index 000000000..5181f8f8f --- /dev/null +++ b/spec/javascripts/app/models/reshare_spec.js @@ -0,0 +1,20 @@ +describe("app.models.Reshare", function(){ + describe("rootPost", function(){ + beforeEach(function(){ + this.reshare = new app.models.Reshare({root: {a:"namaste", be : "aloha", see : "community"}}) + }); + + it("should be the root attrs", function(){ + expect(this.reshare.rootPost().get("be")).toBe("aloha") + }); + + it("should return a post", function(){ + expect(this.reshare.rootPost() instanceof app.models.Post).toBeTruthy() + }); + + it("does not create a new object every time", function(){ + expect(this.reshare.rootPost()).toBe(this.reshare.rootPost()) + }); + }); +}); + diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index 93d670009..8f060bc54 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -15,6 +15,12 @@ describe("app.views.Feedback", function(){ this.view = new app.views.Feedback({model: this.post}); }); + describe("initialization", function(){ + it("sets the model as the reshareable post", function(){ + expect(this.view.reshareablePost).toBe(this.post); + }) + }) + describe(".render", function(){ beforeEach(function(){ this.link = function(){ return this.view.$(".like_action"); } @@ -120,15 +126,15 @@ describe("app.views.Feedback", function(){ it("displays a confirmation dialog", function(){ spyOn(window, "confirm") - this.view.$(".reshare_action").first().click(); expect(window.confirm).toHaveBeenCalled(); }) - it("creates a reshare if the confirmation dialog is accepted", function(){ + it("reshares the reshareablePost", function(){ spyOn(window, "confirm").andReturn(true); - - expect(this.view.resharePost().constructor).toBe(app.models.Reshare); + spyOn(this.view.reshareablePost, "reshare") + this.view.$(".reshare_action").first().click(); + expect(this.view.reshareablePost.reshare).toHaveBeenCalled(); }) }) }) diff --git a/spec/javascripts/app/views/reshare_feedback_view_spec.js b/spec/javascripts/app/views/reshare_feedback_view_spec.js new file mode 100644 index 000000000..5bcbc0fb2 --- /dev/null +++ b/spec/javascripts/app/views/reshare_feedback_view_spec.js @@ -0,0 +1,24 @@ +describe("app.views.ReshareFeedback", function(){ + beforeEach(function(){ + var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"]; + this.reshare = new app.models.Reshare(_.extend(posts[1], {public : true})); + this.view = new app.views.ReshareFeedback({model : this.reshare }).render() + }) + + it("inherits from feedback view", function(){ + expect(this.view instanceof app.views.Feedback).toBeTruthy() + }) + + it("sets Up the root Post as the reshareable post", function(){ + expect(this.view.reshareablePost).toBe(this.reshare.rootPost()) + }) + + it("reshares the rootPost", function(){ + spyOn(window, "confirm").andReturn(true); + spyOn(this.reshare.rootPost(), "reshare") + console.log(this.view.el) + this.view.$(".reshare_action").first().click(); + expect(this.reshare.rootPost().reshare).toHaveBeenCalled(); + }) + +})