Skip to content
Extraits de code Groupes Projets
Valider 4ad58bdf rédigé par Dennis Collinson's avatar Dennis Collinson
Parcourir les fichiers

refactoring backbone urls

parent 7cafac8c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
app.collections.Comments = Backbone.Collection.extend({ app.collections.Comments = Backbone.Collection.extend({
model: app.models.Comment model: app.models.Comment,
initialize : function(models, options) {
this.url = "/posts/" + options.post.id + "/comments" //not delegating to post.url() because when it is in a stream collection it delegates to that url
}
}); });
app.collections.Likes = Backbone.Collection.extend({ app.collections.Likes = Backbone.Collection.extend({
model: app.models.Like model: app.models.Like,
initialize : function(models, options) {
this.url = "/posts/" + options.post.id + "/likes" //not delegating to post.url() because when it is in a stream collection it delegates to that url
}
}); });
app.models.Post = Backbone.Model.extend({ app.models.Post = Backbone.Model.extend({
urlRoot : "/posts",
initialize : function() { initialize : function() {
this.comments = new app.collections.Comments(this.get("last_three_comments")); this.comments = new app.collections.Comments(this.get("last_three_comments"), {post : this});
this.comments.url = this.url() + '/comments'; this.likes = new app.collections.Likes(this.get("user_like"), { post : this}); // load in the user like initially
this.likes = new app.collections.Likes(this.get("user_like")); // load in the user like initially
this.likes.url = this.url() + '/likes';
}, },
url : function() { createdAt : function() {
if(this.id) { return new Date(this.get("created_at")) / 1000;
return "/posts/" + this.id;
} else {
return "/posts"
}
}, },
reshareUrl : "/reshares", createReshareUrl : "/reshares",
reshare : function(){ reshare : function(){
return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")}); return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")});
}, },
...@@ -33,17 +28,8 @@ app.models.Post = Backbone.Model.extend({ ...@@ -33,17 +28,8 @@ app.models.Post = Backbone.Model.extend({
} }
}, },
createdAt : function() {
return new Date(this.get("created_at")) / 1000;
},
likeUrl : function(){
return this.url() + "/likes"
},
like : function() { like : function() {
this.set({ user_like : this.likes.create({}, {url : this.likeUrl()}) }); this.set({ user_like : this.likes.create() });
}, },
unlike : function() { unlike : function() {
......
...@@ -18,7 +18,7 @@ app.views.Feedback = app.views.StreamObject.extend({ ...@@ -18,7 +18,7 @@ app.views.Feedback = app.views.StreamObject.extend({
if(!window.confirm("Reshare " + this.model.reshareAuthor().name + "'s post?")) { return } if(!window.confirm("Reshare " + this.model.reshareAuthor().name + "'s post?")) { return }
var reshare = this.model.reshare() var reshare = this.model.reshare()
reshare.save({}, { reshare.save({}, {
url: this.model.reshareUrl, url: this.model.createReshareUrl,
success : function(){ success : function(){
app.stream.collection.add(reshare); app.stream.collection.add(reshare);
} }
......
describe("app.collections.comments", function(){
describe("url", function(){
it("should user the post id", function(){
var post =new app.models.Post({id : 5})
var collection = new app.collections.Comments([], {post : post})
expect(collection.url).toBe("/posts/5/comments")
})
})
})
describe("app.collections.Likes", function(){
describe("url", function(){
it("should user the post id", function(){
var post =new app.models.Post({id : 5})
var collection = new app.collections.Likes([], {post : post})
expect(collection.url).toBe("/posts/5/likes")
})
})
})
...@@ -3,6 +3,15 @@ describe("app.models.Post", function() { ...@@ -3,6 +3,15 @@ describe("app.models.Post", function() {
this.post = new app.models.Post(); this.post = new app.models.Post();
}) })
describe("url", function(){
it("should be /posts when it doesn't have an id", function(){
expect(new app.models.Post().url()).toBe("/posts")
})
it("should be /posts/id when it doesn't have an id", function(){
expect(new app.models.Post({id: 5}).url()).toBe("/posts/5")
})
})
describe("createdAt", function() { describe("createdAt", function() {
it("returns the post's created_at as an integer", function() { it("returns the post's created_at as an integer", function() {
var date = new Date; var date = new Date;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter