Commit 76226d3

mo khan <mo@mokhan.ca>
2014-11-30 06:34:46
add index view to capture scroll event and load more results.
1 parent bc78e08
Changed files (4)
app
controllers
templates
views
tests
unit
views
app/controllers/cakes/index.js
@@ -4,7 +4,7 @@ export default Ember.ArrayController.extend({
   page: 1,
   perPage: 18,
   actions: {
-    nextPage: function(){
+    more: function(){
       this.set('perPage', this.get('perPage') + 18);
       var that = this;
       this.store.find('cake', { page: 1, per_page: this.get('perPage') }).then(function(cakes){
app/templates/cakes/index.hbs
@@ -8,5 +8,3 @@
   {{/each}}
 </div>
 {{outlet}}
-
-<button {{action 'nextPage'}}>More...</button>
app/views/cakes/index.js
@@ -0,0 +1,26 @@
+import Ember from 'ember';
+
+export default Ember.View.extend({
+  didInsertElement: function(){
+    var view = this;
+    Ember.$(window).bind('scroll', function(){
+      view.didScroll();
+    });
+  },
+
+  willDestroyElement: function(){
+    Ember.$(window).unbind('scroll');
+  },
+
+  didScroll: function(){
+    if(this.isScrolledToBottom()) {
+      this.get('controller').send('more');
+    }
+  },
+
+  isScrolledToBottom: function() {
+    var distanceToTop = Ember.$(document).height() - Ember.$(window).height();
+    var top = Ember.$(document).scrollTop();
+    return top === distanceToTop;
+  }
+});
tests/unit/views/cakes/index-test.js
@@ -0,0 +1,12 @@
+import {
+  moduleFor,
+  test
+} from 'ember-qunit';
+
+moduleFor('view:cakes/index', 'CakesIndexView');
+
+// Replace this with your real tests.
+test('it exists', function() {
+  var view = this.subject();
+  ok(view);
+});