Merge branch 'stream-for-photos'

This commit is contained in:
danielgrippi 2012-04-18 18:36:34 -07:00
commit bf62709c1d
10 changed files with 56 additions and 161 deletions

View file

@ -1,69 +0,0 @@
app.models.Photos = Backbone.Model.extend({
initialize : function(){
this.photos = new app.collections.Photos([], this.photoOptions());
},
photoOptions :function(){
var order = this.sortOrder();
return {
comparator : function(photo) { return -photo[order](); }
}
},
url : function() {
return _.any(this.photos.models) ? this.timeFilteredPath() : this.basePath()
},
_fetching : false,
fetch : function(){
if(this._fetching) { return false; }
var self = this;
// we're fetching the collection... there is probably a better way to do this
self._fetching = true;
this.photos
.fetch({
add : true,
url : self.url()
})
.done(
function(resp){
// we're done fetching... there is probably a better way to handle this
self._fetching = false;
self.trigger("fetched", self);
// all loaded?
if(resp.photos && resp.photos.length == 0) {
self.trigger("allPostsLoaded", self);
}
}
);
return this;
},
basePath : function(){
return document.location.pathname;
},
timeFilteredPath : function(){
return this.basePath() + "?max_time=" + this.maxTime();
},
maxTime: function(){
var lastPost = _.last(this.photos.models);
return lastPost[this.sortOrder()]()
},
sortOrder : function() {
return "createdAt";
},
add : function(models){
this.photos.add(models)
}
});

View file

@ -1,24 +1,27 @@
//= require ../collections/posts
//= require ../collections/photos
app.models.Stream = Backbone.Collection.extend({
initialize : function(){
this.posts = new app.collections.Posts([], this.postOptions());
initialize : function(models, options){
var collection = app.collections.Posts;
if( options && options.collection ) collection = options.collection;
this.items = new collection([], this.collectionOptions());
},
postOptions :function(){
collectionOptions :function(){
var order = this.sortOrder();
return {
comparator : function(post) { return -post[order](); }
comparator : function(item) { return -item[order](); }
}
},
url : function(){
return _.any(this.posts.models) ? this.timeFilteredPath() : this.basePath()
return _.any(this.items.models) ? this.timeFilteredPath() : this.basePath()
},
fetch: function() {
if(this.isFetching()){ return false }
var url = this.url()
this.deferred = this.posts.fetch({
this.deferred = this.items.fetch({
add : true,
url : url
}).done(_.bind(this.triggerFetchedEvents, this))
@ -31,8 +34,9 @@ app.models.Stream = Backbone.Collection.extend({
triggerFetchedEvents : function(resp){
this.trigger("fetched", this);
// all loaded?
if(resp.posts && (resp.posts.author || resp.posts.length == 0)) {
this.trigger("allPostsLoaded", this);
var respItems = this.items.parse(resp);
if(respItems && (respItems.author || respItems.length == 0)) {
this.trigger("allItemsLoaded", this);
}
},
@ -45,7 +49,7 @@ app.models.Stream = Backbone.Collection.extend({
},
maxTime: function(){
var lastPost = _.last(this.posts.models);
var lastPost = _.last(this.items.models);
return lastPost[this.sortOrder()]()
},
@ -54,6 +58,6 @@ app.models.Stream = Backbone.Collection.extend({
},
add : function(models){
this.posts.add(models)
this.items.add(models)
}
});

View file

@ -28,17 +28,19 @@ app.Router = Backbone.Router.extend({
app.stream = new app.models.Stream();
app.stream.fetch();
app.page = new app.views.Stream({model : app.stream});
app.publisher = app.publisher || new app.views.Publisher({collection : app.stream.posts});
app.publisher = app.publisher || new app.views.Publisher({collection : app.stream.items});
var streamFacesView = new app.views.StreamFaces({collection : app.stream.posts});
var streamFacesView = new app.views.StreamFaces({collection : app.stream.items});
$("#main_stream").html(app.page.render().el);
$('#selected_aspect_contacts .content').html(streamFacesView.render().el);
},
photos : function() {
app.photos = new app.models.Photos();
app.photos = new app.models.Stream([], {collection: app.collections.Photos});
app.page = new app.views.Photos({model : app.photos});
$("#main_stream").html(app.page.render().el);
},

View file

@ -1,52 +1,16 @@
app.views.Photos = Backbone.View.extend({
events : {},
app.views.Photos = Backbone.View.extend(_.extend({
initialize : function(options) {
this.photos = this.model;
this.collection = this.model.photos;
this.stream = this.model;
this.collection = this.stream.items;
this.setupEvents();
this.setupLightbox();
// viable for extraction
this.stream.fetch();
this.setupLightbox()
this.setupInfiniteScroll()
},
setupEvents : function(){
this.photos.bind("fetched", this.removeLoader, this)
this.collection.bind("add", this.addPhoto, this);
},
addPhoto : function(photo) {
var photoView = new app.views.Photo({ model: photo });
$(this.el)[
(this.collection.at(0).id == photo.id)
? "prepend"
: "append"
](photoView.render().el);
return this;
},
render : function(evt) {
if(evt) {evt.preventDefault(); }
if(this.model.fetch()) {
this.appendLoader();
};
return this;
},
appendLoader: function(){
$("#paginate").html($("<img>", {
src : "/assets/static-loader.png",
"class" : "loader"
}));
},
removeLoader: function() {
$("#paginate").empty();
},
postClass : app.views.Photo,
setupLightbox : function(){
this.lightbox = Diaspora.BaseWidget.instantiate("Lightbox");
@ -55,6 +19,5 @@ app.views.Photos = Backbone.View.extend({
imageSelector: 'img.photo'
});
$(this.el).delegate("a.photo-link", "click", this.lightbox.lightboxImageClicked);
},
});
}
}, app.views.infiniteScrollMixin));

View file

@ -8,7 +8,7 @@ app.views.StreamFaces = app.views.Base.extend({
initialize : function(){
this.updatePeople()
app.stream.posts.bind("add", this.updatePeople, this)
app.stream.items.bind("add", this.updatePeople, this)
},
presenter : function() {

View file

@ -1,7 +1,8 @@
app.views.Stream = Backbone.View.extend(_.extend({
initialize: function(options) {
this.stream = this.model
this.collection = this.model.posts
this.collection = this.stream.items
this.postViews = []
this.setupNSFW()

View file

@ -9,14 +9,14 @@ describe("app.models.Stream", function() {
beforeEach(function(){
postFetch = new $.Deferred()
spyOn(this.stream.posts, "fetch").andCallFake(function(){
spyOn(this.stream.items, "fetch").andCallFake(function(){
return postFetch
})
})
it("it fetches posts from the window's url, and ads them to tthe collection", function() {
it("it fetches posts from the window's url, and ads them to the collection", function() {
this.stream.fetch()
expect(this.stream.posts.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath});
expect(this.stream.items.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath});
});
it("returns the json path with max_time if the collection has models", function() {
@ -25,7 +25,7 @@ describe("app.models.Stream", function() {
this.stream.add(post);
this.stream.fetch()
expect(this.stream.posts.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath + "?max_time=1234"});
expect(this.stream.items.fetch).toHaveBeenCalledWith({ add : true, url : this.expectedPath + "?max_time=1234"});
});
it("triggers fetched on the stream when it is fetched", function(){
@ -36,17 +36,17 @@ describe("app.models.Stream", function() {
expect(fetchedSpy).toHaveBeenCalled()
})
it("triggers allPostsLoaded on the stream when zero posts are returned", function(){
it("triggers allItemsLoaded on the stream when zero posts are returned", function(){
var fetchedSpy = jasmine.createSpy()
this.stream.bind('allPostsLoaded', fetchedSpy)
this.stream.bind('allItemsLoaded', fetchedSpy)
this.stream.fetch()
postFetch.resolve({posts : []})
expect(fetchedSpy).toHaveBeenCalled()
})
it("triggers allPostsLoaded on the stream when a Post is returned", function(){
it("triggers allItemsLoaded on the stream when a Post is returned", function(){
var fetchedSpy = jasmine.createSpy()
this.stream.bind('allPostsLoaded', fetchedSpy)
this.stream.bind('allItemsLoaded', fetchedSpy)
this.stream.fetch()
postFetch.resolve({posts : factory.post().attributes})
expect(fetchedSpy).toHaveBeenCalled()

View file

@ -2,27 +2,31 @@ describe("app.views.Photos", function() {
beforeEach(function() {
loginAs({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
this._photos = $.parseJSON(spec.readFixture("photos_json"))["photos"];
this.photos = $.parseJSON(spec.readFixture("photos_json"))["photos"];
this.photos = new app.models.Photos();
this.photos.add(this._photos);
this.stream = new app.models.Stream([], {collection: app.collections.Photos});
this.stream.add(this.photos);
this.view = new app.views.Photos({model : this.photos});
this.view = new app.views.Photos({model : this.stream});
// do this manually because we've moved loadMore into render??
this.view.render();
_.each(this.view.collection.models, function(photo) {
this.view.addPhoto(photo);
this.view.addPost(photo);
}, this);
});
describe("initialize", function() {
// nothing there yet
it("binds an infinite scroll listener", function() {
spyOn($.fn, "scroll");
new app.views.Stream({model : this.stream});
expect($.fn.scroll).toHaveBeenCalled();
});
});
describe("#render", function() {
beforeEach(function() {
this.photo = this.photos.photos.models[0];
this.photo = this.stream.items.models[0];
this.photoElement = $(this.view.$("#" + this.photo.get("guid")));
});
@ -32,14 +36,4 @@ describe("app.views.Photos", function() {
});
});
});
describe("removeLoader", function() {
it("emptys the pagination div when the stream is fetched", function() {
$("#jasmine_content").append($('<div id="paginate">OMG</div>'));
expect($("#paginate").text()).toBe("OMG");
this.view.photos.trigger("fetched");
expect($("#paginate")).toBeEmpty();
});
});
});

View file

@ -11,7 +11,7 @@ describe("app.views.StreamFaces", function(){
app.stream = new app.models.Stream()
app.stream.add([this.post1, this.post2, this.post3, this.post4, this.post5, this.post6, this.post7]);
this.posts = app.stream.posts
this.posts = app.stream.items
this.view = new app.views.StreamFaces({collection : this.posts})
})

View file

@ -26,7 +26,7 @@ describe("app.views.Stream", function() {
describe("#render", function() {
beforeEach(function() {
this.statusMessage = this.stream.posts.models[0];
this.statusMessage = this.stream.items.models[0];
this.statusElement = $(this.view.$(".stream_element")[0]);
});