From bc0c92ec64fc7c551b4aa33ddbb7f97d0773d78b Mon Sep 17 00:00:00 2001
From: danielgrippi <danielgrippi@gmail.com>
Date: Sat, 31 Dec 2011 13:39:28 -0500
Subject: [PATCH] added inf. scroll to stream; passing inf. scroll cukes

---
 features/step_definitions/custom_web_steps.rb |  2 --
 public/javascripts/app/views/stream_view.js   | 22 +++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb
index bc1d1d9053..3023c32d11 100644
--- a/features/step_definitions/custom_web_steps.rb
+++ b/features/step_definitions/custom_web_steps.rb
@@ -179,8 +179,6 @@ end
 
 And /^I scroll down$/ do
   evaluate_script("window.scrollBy(0,3000000)")
-  sleep 1
-  wait_until(30) { evaluate_script('$("#infscr-loading:visible").length') == 0 }
   step "I wait for the ajax to finish"
 end
 
diff --git a/public/javascripts/app/views/stream_view.js b/public/javascripts/app/views/stream_view.js
index af54a3da9d..2631fcde6e 100644
--- a/public/javascripts/app/views/stream_view.js
+++ b/public/javascripts/app/views/stream_view.js
@@ -9,9 +9,26 @@ app.views.Stream = Backbone.View.extend({
 
     this.publisher = new app.views.Publisher({collection : this.collection});
 
+    // inf scroll
+    // we're using this._loading to keep track of backbone's collection
+    //   fetching state... is there a better way to do this?
+    var throttledScroll = _.throttle($.proxy(this.infScroll, this), 200);
+    $(window).scroll(throttledScroll);
+
     return this;
   },
 
+  infScroll : function(options) {
+    var $window = $(window);
+    var distFromTop = $window.height() + $window.scrollTop();
+    var distFromBottom = $(document).height() - distFromTop;
+    var bufferPx = 300;
+
+    if(distFromBottom < bufferPx && !this._loading) {
+      this.render();
+    }
+  },
+
   prependPost : function(post) {
     var postView = new app.views.Post({ model: post });
     $(this.el).prepend(postView.render().el);
@@ -32,6 +49,8 @@ app.views.Stream = Backbone.View.extend({
       href: this.collection.url(),
       id: "paginate"
     }).text('Load more posts'));
+
+    this._loading = false;
   },
 
   render : function(evt) {
@@ -39,6 +58,9 @@ app.views.Stream = Backbone.View.extend({
 
     var self = this;
     self.addLoader();
+
+    this._loading = true;
+
     self.collection.fetch({
       add: true,
       success: $.proxy(this.collectionFetched, self)
-- 
GitLab