diff --git a/app/assets/javascripts/app/models/stream.js b/app/assets/javascripts/app/models/stream.js index f18fca61b..3873f8650 100644 --- a/app/assets/javascripts/app/models/stream.js +++ b/app/assets/javascripts/app/models/stream.js @@ -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) } }); diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index dd8f4c6e2..064cdd523 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -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); }, diff --git a/app/assets/javascripts/app/views/photos_view.js b/app/assets/javascripts/app/views/photos_view.js index 74cab7757..d20b1e224 100644 --- a/app/assets/javascripts/app/views/photos_view.js +++ b/app/assets/javascripts/app/views/photos_view.js @@ -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(); @@ -33,7 +33,7 @@ app.views.Photos = Backbone.View.extend({ if(this.model.fetch()) { this.appendLoader(); }; - + return this; }, @@ -57,4 +57,4 @@ app.views.Photos = Backbone.View.extend({ $(this.el).delegate("a.photo-link", "click", this.lightbox.lightboxImageClicked); }, -}); \ No newline at end of file +}); diff --git a/app/assets/javascripts/app/views/stream_faces_view.js b/app/assets/javascripts/app/views/stream_faces_view.js index 70fa6055b..949e8d0b0 100644 --- a/app/assets/javascripts/app/views/stream_faces_view.js +++ b/app/assets/javascripts/app/views/stream_faces_view.js @@ -1,5 +1,5 @@ app.views.StreamFaces = app.views.Base.extend({ - + templateName : "stream-faces", className : "stream-faces", @@ -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() { @@ -26,4 +26,4 @@ app.views.StreamFaces = app.views.Base.extend({ this.render(); } -}); \ No newline at end of file +}); diff --git a/app/assets/javascripts/app/views/stream_view.js b/app/assets/javascripts/app/views/stream_view.js index a379c1af8..bf4de92f3 100644 --- a/app/assets/javascripts/app/views/stream_view.js +++ b/app/assets/javascripts/app/views/stream_view.js @@ -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) @@ -42,7 +42,7 @@ app.views.Stream = Backbone.View.extend({ this.stream.fetch() this.appendLoader() }, - + appendLoader: function(){ $("#paginate").html($("", { src : "/assets/static-loader.png", diff --git a/spec/javascripts/app/models/stream_spec.js b/spec/javascripts/app/models/stream_spec.js index c7a18f621..363024ae8 100644 --- a/spec/javascripts/app/models/stream_spec.js +++ b/spec/javascripts/app/models/stream_spec.js @@ -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() diff --git a/spec/javascripts/app/views/photos_view_spec.js b/spec/javascripts/app/views/photos_view_spec.js index c96a39df8..51cedb9c7 100644 --- a/spec/javascripts/app/views/photos_view_spec.js +++ b/spec/javascripts/app/views/photos_view_spec.js @@ -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"))); }); @@ -41,5 +41,5 @@ describe("app.views.Photos", function() { expect($("#paginate")).toBeEmpty(); }); }); - + }); diff --git a/spec/javascripts/app/views/stream_faces_view_spec.js b/spec/javascripts/app/views/stream_faces_view_spec.js index 31cbd0eeb..956802492 100644 --- a/spec/javascripts/app/views/stream_faces_view_spec.js +++ b/spec/javascripts/app/views/stream_faces_view_spec.js @@ -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}) }) diff --git a/spec/javascripts/app/views/stream_view_spec.js b/spec/javascripts/app/views/stream_view_spec.js index 4e8730aab..79f397b66 100644 --- a/spec/javascripts/app/views/stream_view_spec.js +++ b/spec/javascripts/app/views/stream_view_spec.js @@ -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]); });