use app.models.Stream instead of app.models.Photos,

change naming a bit to be more general since its posts+photos now
(re #3158)
This commit is contained in:
Florian Staudacher 2012-04-18 16:52:00 +02:00
parent b10aedb615
commit 8eb70c8a80
9 changed files with 39 additions and 35 deletions

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,16 +28,16 @@ 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

@ -4,7 +4,7 @@ app.views.Photos = Backbone.View.extend({
initialize : function(options) {
this.photos = this.model;
this.collection = this.model.photos;
this.collection = this.model.items;
this.setupEvents();
this.setupLightbox();

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,7 @@
app.views.Stream = Backbone.View.extend({
initialize: function(options) {
this.stream = this.model
this.collection = this.model.posts
this.collection = this.model.items
this.setupEvents()
this.setupInfiniteScroll()
@ -11,7 +11,7 @@ app.views.Stream = Backbone.View.extend({
setupEvents : function(){
this.stream.bind("fetched", this.removeLoader, this)
this.stream.bind("allPostsLoaded", this.unbindInfScroll, this)
this.stream.bind("allItemsLoaded", this.unbindInfScroll, this)
this.collection.bind("add", this.addPost, this);
app.currentUser.bind("nsfwChanged", reRenderPostViews, this)

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

@ -4,7 +4,7 @@ describe("app.views.Photos", function() {
this._photos = $.parseJSON(spec.readFixture("photos_json"))["photos"];
this.photos = new app.models.Photos();
this.photos = new app.models.Stream([], {collection: app.collections.Photos});
this.photos.add(this._photos);
this.view = new app.views.Photos({model : this.photos});
@ -22,7 +22,7 @@ describe("app.views.Photos", function() {
describe("#render", function() {
beforeEach(function() {
this.photo = this.photos.photos.models[0];
this.photo = this.photos.items.models[0];
this.photoElement = $(this.view.$("#" + this.photo.get("guid")));
});

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]);
});