You can now like "reshares", stream is polymorphic
This commit is contained in:
parent
86c8f99188
commit
9cde06a628
13 changed files with 51 additions and 128 deletions
|
|
@ -9,7 +9,10 @@ app.collections.Stream = Backbone.Collection.extend({
|
||||||
return path;
|
return path;
|
||||||
},
|
},
|
||||||
|
|
||||||
model: app.models.Post,
|
model: function(attrs, options) {
|
||||||
|
var modelClass = app.models[attrs.post_type] || app.models.Post
|
||||||
|
return new modelClass(attrs, options);
|
||||||
|
},
|
||||||
|
|
||||||
parse: function(resp){
|
parse: function(resp){
|
||||||
return resp.posts;
|
return resp.posts;
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1 @@
|
||||||
app.models.Like = Backbone.Model.extend({
|
app.models.Like = Backbone.Model.extend({ })
|
||||||
})
|
|
||||||
|
|
|
||||||
|
|
@ -15,14 +15,13 @@ app.models.Post = Backbone.Model.extend({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
reshareUrl : "reshares/",
|
||||||
reshare : function(){
|
reshare : function(){
|
||||||
var reshare = new app.models.Reshare();
|
return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")});
|
||||||
reshare.save({root_guid : this.baseGuid()}, {
|
},
|
||||||
success : function(){
|
|
||||||
app.stream.collection.add(reshare.toJSON());
|
reshareAuthor : function(){
|
||||||
}
|
return this.get("author")
|
||||||
});
|
|
||||||
return reshare;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleLike : function() {
|
toggleLike : function() {
|
||||||
|
|
@ -35,23 +34,16 @@ app.models.Post = Backbone.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
createdAt : function() {
|
createdAt : function() {
|
||||||
return +new Date(this.get("created_at")) / 1000;
|
return new Date(this.get("created_at")) / 1000;
|
||||||
},
|
},
|
||||||
|
|
||||||
baseGuid : function() {
|
|
||||||
if(this.get("root")){
|
likeUrl : function(){
|
||||||
return this.get("root").guid;
|
return this.url() + "/likes"
|
||||||
} else {
|
|
||||||
return this.get("guid");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
baseAuthor : function() {
|
like : function() {
|
||||||
if(this.get("root")){
|
this.set({ user_like : this.likes.create({}, {url : this.likeUrl()}) });
|
||||||
return this.get("root").author;
|
|
||||||
} else {
|
|
||||||
return this.get("author");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
unlike : function() {
|
unlike : function() {
|
||||||
|
|
@ -60,9 +52,5 @@ app.models.Post = Backbone.Model.extend({
|
||||||
|
|
||||||
likeModel.destroy();
|
likeModel.destroy();
|
||||||
this.set({ user_like : null });
|
this.set({ user_like : null });
|
||||||
},
|
|
||||||
|
|
||||||
like : function() {
|
|
||||||
this.set({ user_like : this.likes.create() });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
app.models.Reshare = app.models.Post.extend({
|
app.models.Reshare = app.models.Post.extend({
|
||||||
url : function() { return "/reshares"; },
|
|
||||||
|
|
||||||
rootPost : function(){
|
rootPost : function(){
|
||||||
this._rootPost = this._rootPost || new app.models.Post(this.get("root"))
|
this._rootPost = this._rootPost || new app.models.Post(this.get("root"))
|
||||||
return this._rootPost
|
return this._rootPost
|
||||||
|
},
|
||||||
|
|
||||||
|
reshare : function(){
|
||||||
|
this.rootPost().reshare()
|
||||||
|
},
|
||||||
|
|
||||||
|
reshareAuthor : function(){
|
||||||
|
return this.rootPost().reshareAuthor()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1 @@
|
||||||
app.models.StatusMessage = app.models.Post.extend({
|
app.models.StatusMessage = app.models.Post.extend({ });
|
||||||
url : function() { return "/status_messages"; }
|
|
||||||
});
|
|
||||||
|
|
|
||||||
|
|
@ -13,14 +13,15 @@ app.views.Feedback = app.views.StreamObject.extend({
|
||||||
this.model.toggleLike();
|
this.model.toggleLike();
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize: function(options){
|
|
||||||
this.setupRenderEvents();
|
|
||||||
this.reshareablePost = options.model;
|
|
||||||
},
|
|
||||||
|
|
||||||
resharePost : function(evt){
|
resharePost : function(evt){
|
||||||
if(evt) { evt.preventDefault(); }
|
if(evt) { evt.preventDefault(); }
|
||||||
if(!window.confirm("Reshare " + this.reshareablePost.baseAuthor().name + "'s post?")) { return }
|
if(!window.confirm("Reshare " + this.model.reshareAuthor().name + "'s post?")) { return }
|
||||||
this.reshareablePost.reshare();
|
var reshare = this.model.reshare()
|
||||||
|
reshare.save({}, {
|
||||||
|
url: this.model.reshareUrl,
|
||||||
|
success : function(){
|
||||||
|
app.stream.collection.add(reshare);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -35,12 +35,7 @@ app.views.Post = app.views.StreamObject.extend({
|
||||||
|
|
||||||
feedbackView : function(){
|
feedbackView : function(){
|
||||||
if(!window.app.user().current_user ) { return null }
|
if(!window.app.user().current_user ) { return null }
|
||||||
var feedbackViewClass = this.resharedContent() ? app.views.ReshareFeedback : app.views.Feedback
|
return new app.views.Feedback({model : this.model});
|
||||||
return new feedbackViewClass({model : this.model});
|
|
||||||
},
|
|
||||||
|
|
||||||
resharedContent : function(){
|
|
||||||
return this.model.get('root')
|
|
||||||
},
|
},
|
||||||
|
|
||||||
postContentView: function(){
|
postContentView: function(){
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
@ -13,22 +13,6 @@ describe("app.models.Post", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("baseGuid", function(){
|
|
||||||
it("returns the post's guid if the post does not have a root", function() {
|
|
||||||
this.post.attributes.root = null;
|
|
||||||
this.post.attributes.guid = "abcd";
|
|
||||||
|
|
||||||
expect(this.post.baseGuid()).toBe("abcd")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the post's root guid if the post has a root", function() {
|
|
||||||
this.post.attributes.root = {guid : "1234"}
|
|
||||||
this.post.attributes.guid = "abcd";
|
|
||||||
|
|
||||||
expect(this.post.baseGuid()).toBe("1234")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("toggleLike", function(){
|
describe("toggleLike", function(){
|
||||||
it("calls unliked when the user_like exists", function(){
|
it("calls unliked when the user_like exists", function(){
|
||||||
this.post.set({user_like : "123"});
|
this.post.set({user_like : "123"});
|
||||||
|
|
@ -67,20 +51,4 @@ describe("app.models.Post", function() {
|
||||||
expect(app.models.Like.prototype.destroy).toHaveBeenCalled();
|
expect(app.models.Like.prototype.destroy).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("baseAuthor", function(){
|
|
||||||
it("returns the post's guid if the post does not have a root", function() {
|
|
||||||
this.post.attributes.root = null;
|
|
||||||
this.post.attributes.author = "abcd";
|
|
||||||
|
|
||||||
expect(this.post.baseAuthor()).toBe("abcd")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the post's root guid if the post has a root", function() {
|
|
||||||
this.post.attributes.root = {author : "1234"}
|
|
||||||
this.post.attributes.author = "abcd";
|
|
||||||
|
|
||||||
expect(this.post.baseAuthor()).toBe("1234")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
describe("app.models.Reshare", function(){
|
describe("app.models.Reshare", function(){
|
||||||
describe("rootPost", function(){
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
this.reshare = new app.models.Reshare({root: {a:"namaste", be : "aloha", see : "community"}})
|
this.reshare = new app.models.Reshare({root: {a:"namaste", be : "aloha", see : "community"}})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("rootPost", function(){
|
||||||
it("should be the root attrs", function(){
|
it("should be the root attrs", function(){
|
||||||
expect(this.reshare.rootPost().get("be")).toBe("aloha")
|
expect(this.reshare.rootPost().get("be")).toBe("aloha")
|
||||||
});
|
});
|
||||||
|
|
@ -16,5 +16,13 @@ describe("app.models.Reshare", function(){
|
||||||
expect(this.reshare.rootPost()).toBe(this.reshare.rootPost())
|
expect(this.reshare.rootPost()).toBe(this.reshare.rootPost())
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe(".reshare", function(){
|
||||||
|
it("reshares the root post", function(){
|
||||||
|
spyOn(this.reshare.rootPost(), "reshare")
|
||||||
|
this.reshare.reshare()
|
||||||
|
expect(this.reshare.rootPost().reshare).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,6 @@ describe("app.views.Feedback", function(){
|
||||||
this.view = new app.views.Feedback({model: this.post});
|
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(){
|
describe(".render", function(){
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
|
|
@ -130,11 +125,11 @@ describe("app.views.Feedback", function(){
|
||||||
expect(window.confirm).toHaveBeenCalled();
|
expect(window.confirm).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
|
|
||||||
it("reshares the reshareablePost", function(){
|
it("reshares the model", function(){
|
||||||
spyOn(window, "confirm").andReturn(true);
|
spyOn(window, "confirm").andReturn(true);
|
||||||
spyOn(this.view.reshareablePost, "reshare")
|
spyOn(this.view.model.reshare(), "save")
|
||||||
this.view.$(".reshare_action").first().click();
|
this.view.$(".reshare_action").first().click();
|
||||||
expect(this.view.reshareablePost.reshare).toHaveBeenCalled();
|
expect(this.view.model.reshare().save).toHaveBeenCalled();
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,6 @@ describe("app.views.Post", function(){
|
||||||
this.reshare = this.collection.models[1];
|
this.reshare = this.collection.models[1];
|
||||||
})
|
})
|
||||||
|
|
||||||
context("for a reshare", function(){
|
|
||||||
it("should display ReshareFeedback", function(){
|
|
||||||
spyOn(app.views, "ReshareFeedback").andReturn(stubView("these are special reshare actions"));
|
|
||||||
var view = new app.views.Post({model : this.reshare}).render();
|
|
||||||
expect(view.$(".feedback").text().trim()).toBe("these are special reshare actions");
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("displays a reshare count", function(){
|
it("displays a reshare count", function(){
|
||||||
this.statusMessage.set({reshares_count : 2})
|
this.statusMessage.set({reshares_count : 2})
|
||||||
var view = new app.views.Post({model : this.statusMessage}).render();
|
var view = new app.views.Post({model : this.statusMessage}).render();
|
||||||
|
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
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();
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
|
||||||
Loading…
Reference in a new issue