Skip to content
Extraits de code Groupes Projets
Valider b583a7a4 rédigé par danielgrippi's avatar danielgrippi
Parcourir les fichiers

Put dennis's changes back in

Revert "Revert "refactoring backbone urls""

This reverts commit 838da1fd.

Revert "revert 9b1d64bb as it is causing inf. scroll to break (stream model now has a post collection)"

This reverts commit 2a69c0eb.
parent 3fa25927
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 164 ajouts et 121 suppressions
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.collections.Stream = Backbone.Collection.extend({ app.collections.Posts = Backbone.Collection.extend({
url: function() { url : "/posts",
var path = document.location.pathname;
if(this.models.length) {
path += "?max_time=" + _.last(this.models).createdAt();
}
return path;
},
model: function(attrs, options) { model: function(attrs, options) {
var modelClass = app.models[attrs.post_type] || app.models.Post var modelClass = app.models[attrs.post_type] || app.models.Post
......
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() {
......
...@@ -13,10 +13,11 @@ app.Router = Backbone.Router.extend({ ...@@ -13,10 +13,11 @@ app.Router = Backbone.Router.extend({
}, },
stream : function() { stream : function() {
app.stream = new app.views.Stream().render(); app.stream = new app.models.Stream()
$("#main_stream").html(app.stream.el); app.page = new app.views.Stream().render();
$("#main_stream").html(app.page.el);
var streamFacesView = new app.views.StreamFaces({collection : app.stream.collection}).render(); var streamFacesView = new app.views.StreamFaces({collection : app.stream.posts}).render();
$('#selected_aspect_contacts .content').html(streamFacesView.el); $('#selected_aspect_contacts .content').html(streamFacesView.el);
} }
}); });
......
...@@ -18,9 +18,9 @@ app.views.Feedback = app.views.StreamObject.extend({ ...@@ -18,9 +18,9 @@ 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.add(reshare);
} }
}); });
} }
......
...@@ -63,9 +63,9 @@ app.views.Post = app.views.StreamObject.extend({ ...@@ -63,9 +63,9 @@ app.views.Post = app.views.StreamObject.extend({
success : function(){ success : function(){
if(!app.stream) { return } if(!app.stream) { return }
_.each(app.stream.collection.models, function(model){ _.each(app.stream.posts.models, function(model){
if(model.get("author").id == personId) { if(model.get("author").id == personId) {
app.stream.collection.remove(model); app.stream.posts.remove(model);
} }
}) })
} }
......
...@@ -12,7 +12,7 @@ app.views.Publisher = Backbone.View.extend({ ...@@ -12,7 +12,7 @@ app.views.Publisher = Backbone.View.extend({
}, },
initialize : function(){ initialize : function(){
this.collection = this.collection || new app.collections.Stream; this.collection = this.collection || new app.collections.Posts;
return this; return this;
}, },
...@@ -33,7 +33,7 @@ app.views.Publisher = Backbone.View.extend({ ...@@ -33,7 +33,7 @@ app.views.Publisher = Backbone.View.extend({
}, { }, {
url : "/status_messages", url : "/status_messages",
success : function() { success : function() {
app.stream.collection.add(statusMessage.toJSON()); app.stream.posts.add(statusMessage.toJSON());
} }
}); });
......
...@@ -8,7 +8,7 @@ app.views.StreamFaces = app.views.Base.extend({ ...@@ -8,7 +8,7 @@ app.views.StreamFaces = app.views.Base.extend({
initialize : function(){ initialize : function(){
this.updatePeople() this.updatePeople()
this.collection.bind("add", this.updatePeople, this) app.stream.posts.bind("add", this.updatePeople, this)
}, },
presenter : function() { presenter : function() {
......
...@@ -3,46 +3,17 @@ app.views.Stream = Backbone.View.extend({ ...@@ -3,46 +3,17 @@ app.views.Stream = Backbone.View.extend({
"click #paginate": "render" "click #paginate": "render"
}, },
initialize: function() { initialize: function(options) {
this.collection = this.collection || new app.collections.Stream; this.stream = app.stream || new app.models.Stream()
this.collection.bind("add", this.addPost, this); this.collection = this.stream.posts
this.publisher = new app.views.Publisher({collection : this.collection}); this.publisher = new app.views.Publisher({collection : this.collection});
// inf scroll this.stream.bind("fetched", this.collectionFetched, this)
// we're using this._loading to keep track of backbone's collection this.collection.bind("add", this.addPost, this);
// fetching state... is there a better way to do this? this.setupInfiniteScroll()
var throttledScroll = _.throttle($.proxy(this.infScroll, this), 200); this.setupLightbox()
$(window).scroll(throttledScroll);
// lightbox delegation
this.lightbox = Diaspora.BaseWidget.instantiate("Lightbox");
$(this.el).delegate("a.stream-photo-link", "click", this.lightbox.lightboxImageClicked);
return this;
},
infScroll : function() {
if(this.allContentLoaded || this.isLoading()) { return }
var $window = $(window);
var distFromTop = $window.height() + $window.scrollTop();
var distFromBottom = $(document).height() - distFromTop;
var bufferPx = 500;
if(distFromBottom < bufferPx) {
this.render();
}
return this;
},
isLoading : function(){
return this._loading && !this._loading.isResolved();
}, },
allContentLoaded : false,
addPost : function(post) { addPost : function(post) {
var postView = new app.views.Post({ model: post }); var postView = new app.views.Post({ model: post });
...@@ -55,9 +26,15 @@ app.views.Stream = Backbone.View.extend({ ...@@ -55,9 +26,15 @@ app.views.Stream = Backbone.View.extend({
return this; return this;
}, },
collectionFetched: function(collection, response) { isLoading : function(){
this.$("#paginate").remove(); return this._loading && !this._loading.isResolved();
},
allContentLoaded : false,
collectionFetched: function(collection, response) {
this.removeLoader()
if(!collection.parse(response).length || collection.parse(response).length == 0) { if(!collection.parse(response).length || collection.parse(response).length == 0) {
this.allContentLoaded = true; this.allContentLoaded = true;
$(window).unbind('scroll') $(window).unbind('scroll')
...@@ -65,7 +42,7 @@ app.views.Stream = Backbone.View.extend({ ...@@ -65,7 +42,7 @@ app.views.Stream = Backbone.View.extend({
} }
$(this.el).append($("<a>", { $(this.el).append($("<a>", {
href: this.collection.url(), href: this.stream.url(),
id: "paginate" id: "paginate"
}).text('Load more posts')); }).text('Load more posts'));
}, },
...@@ -73,13 +50,8 @@ app.views.Stream = Backbone.View.extend({ ...@@ -73,13 +50,8 @@ app.views.Stream = Backbone.View.extend({
render : function(evt) { render : function(evt) {
if(evt) { evt.preventDefault(); } if(evt) { evt.preventDefault(); }
var self = this; this.addLoader();
self.addLoader(); this._loading = this.stream.fetch();
this._loading = self.collection.fetch({
add: true,
success: $.proxy(this.collectionFetched, self)
});
return this; return this;
}, },
...@@ -95,5 +67,34 @@ app.views.Stream = Backbone.View.extend({ ...@@ -95,5 +67,34 @@ app.views.Stream = Backbone.View.extend({
src : "/images/static-loader.png", src : "/images/static-loader.png",
"class" : "loader" "class" : "loader"
})); }));
} },
removeLoader : function(){
this.$("#paginate").remove();
},
setupLightbox : function(){
this.lightbox = Diaspora.BaseWidget.instantiate("Lightbox");
$(this.el).delegate("a.stream-photo-link", "click", this.lightbox.lightboxImageClicked);
},
setupInfiniteScroll : function() {
var throttledScroll = _.throttle($.proxy(this.infScroll, this), 200);
$(window).scroll(throttledScroll);
},
infScroll : function() {
if(this.allContentLoaded || this.isLoading()) { return }
var $window = $(window);
var distFromTop = $window.height() + $window.scrollTop();
var distFromBottom = $(document).height() - distFromTop;
var bufferPx = 500;
if(distFromBottom < bufferPx) {
this.render();
}
return this;
},
}); });
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")
})
})
})
describe("app.collections.Stream", function() {
describe("url", function() {
var stream = new app.collections.Stream(),
expectedPath = document.location.pathname;
it("returns the correct path", function() {
expect(stream.url()).toEqual(expectedPath);
});
it("returns the json path with max_time if the collection has models", function() {
var post = new app.models.Post();
spyOn(post, "createdAt").andReturn(1234);
stream.add(post);
expect(stream.url()).toEqual(expectedPath + "?max_time=1234");
});
});
});
...@@ -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;
......
describe("app.models.Stream", function() {
beforeEach(function(){
this.stream = new app.models.Stream(),
this.expectedPath = document.location.pathname;
})
describe(".fetch", function() {
var postFetch
beforeEach(function(){
postFetch = new $.Deferred()
spyOn(this.stream.posts, "fetch").andCallFake(function(){
return postFetch
})
})
it("it fetches posts from the window's url, and ads them to tthe collection", function() {
this.stream.fetch()
expect(this.stream.posts.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath});
});
it("returns the json path with max_time if the collection has models", function() {
var post = new app.models.Post();
spyOn(post, "createdAt").andReturn(1234);
this.stream.add(post);
this.stream.fetch()
expect(this.stream.posts.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath + "?max_time=1234"});
});
it("triggers fetched on the stream when it is fetched", function(){
var fetchedSpy = jasmine.createSpy()
this.stream.bind('fetched', fetchedSpy)
this.stream.fetch()
postFetch.resolve()
expect(fetchedSpy).toHaveBeenCalled()
})
});
});
...@@ -13,7 +13,7 @@ describe("app.views.Post", function(){ ...@@ -13,7 +13,7 @@ describe("app.views.Post", function(){
var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"]; var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"];
this.collection = new app.collections.Stream(posts); this.collection = new app.collections.Posts(posts);
this.statusMessage = this.collection.models[0]; this.statusMessage = this.collection.models[0];
this.reshare = this.collection.models[1]; this.reshare = this.collection.models[1];
}) })
......
...@@ -9,8 +9,11 @@ describe("app.views.StreamFaces", function(){ ...@@ -9,8 +9,11 @@ describe("app.views.StreamFaces", function(){
this.post6 = factory.post({author : rebeccaBlack}) this.post6 = factory.post({author : rebeccaBlack})
this.post7 = factory.post({author : rebeccaBlack}) this.post7 = factory.post({author : rebeccaBlack})
this.stream = new app.collections.Stream([this.post1, this.post2, this.post3, this.post4, this.post5, this.post6, this.post7]); app.stream = new app.models.Stream()
this.view = new app.views.StreamFaces({collection : this.stream}) app.stream.add([this.post1, this.post2, this.post3, this.post4, this.post5, this.post6, this.post7]);
this.posts = app.stream.posts
this.view = new app.views.StreamFaces({collection : this.posts})
}) })
it("should take them unique", function(){ it("should take them unique", function(){
...@@ -19,7 +22,7 @@ describe("app.views.StreamFaces", function(){ ...@@ -19,7 +22,7 @@ describe("app.views.StreamFaces", function(){
}) })
it("findsPeople when the collection changes", function(){ it("findsPeople when the collection changes", function(){
this.stream.add(factory.post({author : factory.author({name : "Harriet Tubman"})})) this.posts.add(factory.post({author : factory.author({name : "Harriet Tubman"})}))
expect(this.view.people.length).toBe(6) expect(this.view.people.length).toBe(6)
}) })
...@@ -39,8 +42,8 @@ describe("app.views.StreamFaces", function(){ ...@@ -39,8 +42,8 @@ describe("app.views.StreamFaces", function(){
it("rerenders when people are added, but caps to 15 people", function(){ it("rerenders when people are added, but caps to 15 people", function(){
var posts = _.map(_.range(20), function(){ return factory.post()}) var posts = _.map(_.range(20), function(){ return factory.post()})
this.stream.reset(posts) //add 20 posts silently to the collection this.posts.reset(posts) //add 20 posts silently to the collection
this.stream.add(factory.post) //trigger an update this.posts.add(factory.post) //trigger an update
expect(this.view.$("img").length).toBe(15) expect(this.view.$("img").length).toBe(15)
}) })
}) })
......
...@@ -4,14 +4,17 @@ describe("app.views.Stream", function(){ ...@@ -4,14 +4,17 @@ describe("app.views.Stream", function(){
this.posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"]; this.posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"];
this.collection = new app.collections.Stream(this.posts); app.stream = new app.models.Stream()
app.stream.add(this.posts);
this.collection = app.stream.posts
this.view = new app.views.Stream({collection : this.collection}); this.view = new app.views.Stream({collection : this.collection});
app.stream.bind("fetched", this.collectionFetched, this) //untested
// do this manually because we've moved loadMore into render?? // do this manually because we've moved loadMore into render??
this.view.render(); this.view.render();
_.each(this.view.collection.models, function(post){ _.each(this.view.collection.models, function(post){ this.view.addPost(post); }, this);
this.view.addPost(post);
}, this);
}) })
describe("initialize", function(){ describe("initialize", function(){
...@@ -42,7 +45,7 @@ describe("app.views.Stream", function(){ ...@@ -42,7 +45,7 @@ describe("app.views.Stream", function(){
// NOTE: inf scroll happens at 500px // NOTE: inf scroll happens at 500px
beforeEach(function(){ beforeEach(function(){
spyOn(this.view.collection, "fetch") spyOn(this.view.collection, "fetch").andReturn($.Deferred())
}) })
context("when the user is at the bottom of the page", function(){ context("when the user is at the bottom of the page", function(){
......
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