Merge branch 'stream-for-photos'
This commit is contained in:
commit
bf62709c1d
10 changed files with 56 additions and 161 deletions
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
@ -1,24 +1,27 @@
|
||||||
//= require ../collections/posts
|
//= require ../collections/posts
|
||||||
|
//= require ../collections/photos
|
||||||
app.models.Stream = Backbone.Collection.extend({
|
app.models.Stream = Backbone.Collection.extend({
|
||||||
initialize : function(){
|
initialize : function(models, options){
|
||||||
this.posts = new app.collections.Posts([], this.postOptions());
|
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();
|
var order = this.sortOrder();
|
||||||
return {
|
return {
|
||||||
comparator : function(post) { return -post[order](); }
|
comparator : function(item) { return -item[order](); }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
url : function(){
|
url : function(){
|
||||||
return _.any(this.posts.models) ? this.timeFilteredPath() : this.basePath()
|
return _.any(this.items.models) ? this.timeFilteredPath() : this.basePath()
|
||||||
},
|
},
|
||||||
|
|
||||||
fetch: function() {
|
fetch: function() {
|
||||||
if(this.isFetching()){ return false }
|
if(this.isFetching()){ return false }
|
||||||
var url = this.url()
|
var url = this.url()
|
||||||
this.deferred = this.posts.fetch({
|
this.deferred = this.items.fetch({
|
||||||
add : true,
|
add : true,
|
||||||
url : url
|
url : url
|
||||||
}).done(_.bind(this.triggerFetchedEvents, this))
|
}).done(_.bind(this.triggerFetchedEvents, this))
|
||||||
|
|
@ -31,8 +34,9 @@ app.models.Stream = Backbone.Collection.extend({
|
||||||
triggerFetchedEvents : function(resp){
|
triggerFetchedEvents : function(resp){
|
||||||
this.trigger("fetched", this);
|
this.trigger("fetched", this);
|
||||||
// all loaded?
|
// all loaded?
|
||||||
if(resp.posts && (resp.posts.author || resp.posts.length == 0)) {
|
var respItems = this.items.parse(resp);
|
||||||
this.trigger("allPostsLoaded", this);
|
if(respItems && (respItems.author || respItems.length == 0)) {
|
||||||
|
this.trigger("allItemsLoaded", this);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -45,7 +49,7 @@ app.models.Stream = Backbone.Collection.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
maxTime: function(){
|
maxTime: function(){
|
||||||
var lastPost = _.last(this.posts.models);
|
var lastPost = _.last(this.items.models);
|
||||||
return lastPost[this.sortOrder()]()
|
return lastPost[this.sortOrder()]()
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -54,6 +58,6 @@ app.models.Stream = Backbone.Collection.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
add : function(models){
|
add : function(models){
|
||||||
this.posts.add(models)
|
this.items.add(models)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -28,17 +28,19 @@ app.Router = Backbone.Router.extend({
|
||||||
app.stream = new app.models.Stream();
|
app.stream = new app.models.Stream();
|
||||||
app.stream.fetch();
|
app.stream.fetch();
|
||||||
app.page = new app.views.Stream({model : app.stream});
|
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);
|
$("#main_stream").html(app.page.render().el);
|
||||||
$('#selected_aspect_contacts .content').html(streamFacesView.render().el);
|
$('#selected_aspect_contacts .content').html(streamFacesView.render().el);
|
||||||
},
|
},
|
||||||
|
|
||||||
photos : function() {
|
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});
|
app.page = new app.views.Photos({model : app.photos});
|
||||||
|
|
||||||
|
|
||||||
$("#main_stream").html(app.page.render().el);
|
$("#main_stream").html(app.page.render().el);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,16 @@
|
||||||
app.views.Photos = Backbone.View.extend({
|
app.views.Photos = Backbone.View.extend(_.extend({
|
||||||
|
|
||||||
events : {},
|
|
||||||
|
|
||||||
initialize : function(options) {
|
initialize : function(options) {
|
||||||
this.photos = this.model;
|
this.stream = this.model;
|
||||||
this.collection = this.model.photos;
|
this.collection = this.stream.items;
|
||||||
|
|
||||||
this.setupEvents();
|
// viable for extraction
|
||||||
this.setupLightbox();
|
this.stream.fetch();
|
||||||
|
|
||||||
|
this.setupLightbox()
|
||||||
|
this.setupInfiniteScroll()
|
||||||
},
|
},
|
||||||
|
|
||||||
setupEvents : function(){
|
postClass : app.views.Photo,
|
||||||
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();
|
|
||||||
},
|
|
||||||
|
|
||||||
setupLightbox : function(){
|
setupLightbox : function(){
|
||||||
this.lightbox = Diaspora.BaseWidget.instantiate("Lightbox");
|
this.lightbox = Diaspora.BaseWidget.instantiate("Lightbox");
|
||||||
|
|
@ -55,6 +19,5 @@ app.views.Photos = Backbone.View.extend({
|
||||||
imageSelector: 'img.photo'
|
imageSelector: 'img.photo'
|
||||||
});
|
});
|
||||||
$(this.el).delegate("a.photo-link", "click", this.lightbox.lightboxImageClicked);
|
$(this.el).delegate("a.photo-link", "click", this.lightbox.lightboxImageClicked);
|
||||||
},
|
}
|
||||||
|
}, app.views.infiniteScrollMixin));
|
||||||
});
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
app.views.StreamFaces = app.views.Base.extend({
|
app.views.StreamFaces = app.views.Base.extend({
|
||||||
|
|
||||||
templateName : "stream-faces",
|
templateName : "stream-faces",
|
||||||
|
|
||||||
className : "stream-faces",
|
className : "stream-faces",
|
||||||
|
|
@ -8,7 +8,7 @@ app.views.StreamFaces = app.views.Base.extend({
|
||||||
|
|
||||||
initialize : function(){
|
initialize : function(){
|
||||||
this.updatePeople()
|
this.updatePeople()
|
||||||
app.stream.posts.bind("add", this.updatePeople, this)
|
app.stream.items.bind("add", this.updatePeople, this)
|
||||||
},
|
},
|
||||||
|
|
||||||
presenter : function() {
|
presenter : function() {
|
||||||
|
|
@ -26,4 +26,4 @@ app.views.StreamFaces = app.views.Base.extend({
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
app.views.Stream = Backbone.View.extend(_.extend({
|
app.views.Stream = Backbone.View.extend(_.extend({
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.stream = this.model
|
this.stream = this.model
|
||||||
this.collection = this.model.posts
|
this.collection = this.stream.items
|
||||||
|
|
||||||
this.postViews = []
|
this.postViews = []
|
||||||
|
|
||||||
this.setupNSFW()
|
this.setupNSFW()
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,14 @@ describe("app.models.Stream", function() {
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
postFetch = new $.Deferred()
|
postFetch = new $.Deferred()
|
||||||
|
|
||||||
spyOn(this.stream.posts, "fetch").andCallFake(function(){
|
spyOn(this.stream.items, "fetch").andCallFake(function(){
|
||||||
return postFetch
|
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()
|
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() {
|
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.add(post);
|
||||||
|
|
||||||
this.stream.fetch()
|
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(){
|
it("triggers fetched on the stream when it is fetched", function(){
|
||||||
|
|
@ -36,17 +36,17 @@ describe("app.models.Stream", function() {
|
||||||
expect(fetchedSpy).toHaveBeenCalled()
|
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()
|
var fetchedSpy = jasmine.createSpy()
|
||||||
this.stream.bind('allPostsLoaded', fetchedSpy)
|
this.stream.bind('allItemsLoaded', fetchedSpy)
|
||||||
this.stream.fetch()
|
this.stream.fetch()
|
||||||
postFetch.resolve({posts : []})
|
postFetch.resolve({posts : []})
|
||||||
expect(fetchedSpy).toHaveBeenCalled()
|
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()
|
var fetchedSpy = jasmine.createSpy()
|
||||||
this.stream.bind('allPostsLoaded', fetchedSpy)
|
this.stream.bind('allItemsLoaded', fetchedSpy)
|
||||||
this.stream.fetch()
|
this.stream.fetch()
|
||||||
postFetch.resolve({posts : factory.post().attributes})
|
postFetch.resolve({posts : factory.post().attributes})
|
||||||
expect(fetchedSpy).toHaveBeenCalled()
|
expect(fetchedSpy).toHaveBeenCalled()
|
||||||
|
|
|
||||||
|
|
@ -2,27 +2,31 @@ describe("app.views.Photos", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
loginAs({name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
|
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.stream = new app.models.Stream([], {collection: app.collections.Photos});
|
||||||
this.photos.add(this._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??
|
// do this manually because we've moved loadMore into render??
|
||||||
this.view.render();
|
this.view.render();
|
||||||
_.each(this.view.collection.models, function(photo) {
|
_.each(this.view.collection.models, function(photo) {
|
||||||
this.view.addPhoto(photo);
|
this.view.addPost(photo);
|
||||||
}, this);
|
}, this);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("initialize", function() {
|
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() {
|
describe("#render", function() {
|
||||||
beforeEach(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")));
|
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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ describe("app.views.StreamFaces", function(){
|
||||||
|
|
||||||
app.stream = new app.models.Stream()
|
app.stream = new app.models.Stream()
|
||||||
app.stream.add([this.post1, this.post2, this.post3, this.post4, this.post5, this.post6, this.post7]);
|
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})
|
this.view = new app.views.StreamFaces({collection : this.posts})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ describe("app.views.Stream", function() {
|
||||||
|
|
||||||
describe("#render", function() {
|
describe("#render", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.statusMessage = this.stream.posts.models[0];
|
this.statusMessage = this.stream.items.models[0];
|
||||||
this.statusElement = $(this.view.$(".stream_element")[0]);
|
this.statusElement = $(this.view.$(".stream_element")[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue