From 1804b19ccc32cb543bf2bbedd0eac7765a8ad13b Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Sat, 31 Dec 2011 16:23:44 -0500 Subject: [PATCH] DG DH; dry-d up appending/prepending posts to the stream's collection --- app/views/shared/_publisher.html.haml | 2 +- public/javascripts/app/collections/stream.js | 4 +++ public/javascripts/app/views/feedback_view.js | 6 ++-- .../javascripts/app/views/publisher_view.js | 6 ++-- public/javascripts/app/views/stream_view.js | 28 +++++++++---------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/views/shared/_publisher.html.haml b/app/views/shared/_publisher.html.haml index c9c8bda0a..0ee9a7434 100644 --- a/app/views/shared/_publisher.html.haml +++ b/app/views/shared/_publisher.html.haml @@ -81,7 +81,7 @@ - for aspect in all_aspects = aspect_dropdown_list_item(aspect, !all_aspects_selected?(selected_aspects) && selected_aspects.include?(aspect) ) - = status.submit t('.share'), :disabled => publisher_hidden_text.blank?, :disable_with => t('.posting'), :class => 'button creation', :tabindex => 2 + = status.submit t('.share'), :disabled => publisher_hidden_text.blank?, :class => 'button creation', :tabindex => 2 .facebox_content #question_mark_pane diff --git a/public/javascripts/app/collections/stream.js b/public/javascripts/app/collections/stream.js index 59b2e2ac6..70fe1b5da 100644 --- a/public/javascripts/app/collections/stream.js +++ b/public/javascripts/app/collections/stream.js @@ -13,5 +13,9 @@ app.collections.Stream = Backbone.Collection.extend({ parse: function(resp){ return resp.posts; + }, + + comparator : function(post) { + return -post.createdAt(); } }); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index 489deb023..cc6f07156 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -32,10 +32,8 @@ app.views.Feedback = app.views.StreamObject.extend({ if(window.confirm("Reshare " + this.model.baseAuthor().name + "'s post?")) { var reshare = new app.models.Reshare(); reshare.save({root_guid : this.model.baseGuid()}, { - success : function(data){ - var newPost = new app.models.Post(data); - app.stream.collection.add(newPost, {silent : true}); - app.stream.prependPost(newPost); + success : function(){ + app.stream.collection.add(reshare.toJSON()); } }); return reshare; diff --git a/public/javascripts/app/views/publisher_view.js b/public/javascripts/app/views/publisher_view.js index 436752b04..13967168b 100644 --- a/public/javascripts/app/views/publisher_view.js +++ b/public/javascripts/app/views/publisher_view.js @@ -27,10 +27,8 @@ app.views.Publisher = Backbone.View.extend({ "aspect_ids" : serializedForm["aspect_ids[]"], "photos" : serializedForm["photos[]"] }, { - success : function(data) { - var newPost = new app.models.Post(data); - app.stream.collection.add(newPost, {silent : true}); - app.stream.prependPost(newPost); + success : function() { + app.stream.collection.add(statusMessage.toJSON()); } }); diff --git a/public/javascripts/app/views/stream_view.js b/public/javascripts/app/views/stream_view.js index 2631fcde6..60e4f958b 100644 --- a/public/javascripts/app/views/stream_view.js +++ b/public/javascripts/app/views/stream_view.js @@ -5,7 +5,7 @@ app.views.Stream = Backbone.View.extend({ initialize: function() { this.collection = this.collection || new app.collections.Stream; - this.collection.bind("add", this.appendPost, this); + this.collection.bind("add", this.addPost, this); this.publisher = new app.views.Publisher({collection : this.collection}); @@ -19,26 +19,30 @@ app.views.Stream = Backbone.View.extend({ }, infScroll : function(options) { + if(this.isLoading()) { return } + var $window = $(window); var distFromTop = $window.height() + $window.scrollTop(); var distFromBottom = $(document).height() - distFromTop; var bufferPx = 300; - if(distFromBottom < bufferPx && !this._loading) { + if(distFromBottom < bufferPx) { this.render(); } }, - prependPost : function(post) { - var postView = new app.views.Post({ model: post }); - $(this.el).prepend(postView.render().el); - - return this; + isLoading : function(){ + return !this._loading.isResolved(); }, - appendPost: function(post) { + addPost : function(post) { var postView = new app.views.Post({ model: post }); - $(this.el).append(postView.render().el); + + $(this.el)[ + (this.collection.at(0).id == post.id) + ? "prepend" + : "append" + ](postView.render().el); return this; }, @@ -49,8 +53,6 @@ app.views.Stream = Backbone.View.extend({ href: this.collection.url(), id: "paginate" }).text('Load more posts')); - - this._loading = false; }, render : function(evt) { @@ -59,9 +61,7 @@ app.views.Stream = Backbone.View.extend({ var self = this; self.addLoader(); - this._loading = true; - - self.collection.fetch({ + this._loading = self.collection.fetch({ add: true, success: $.proxy(this.collectionFetched, self) });