Skip to content

Instantly share code, notes, and snippets.

@duvillierA
Last active August 29, 2015 14:14
Show Gist options
  • Save duvillierA/ff6b95a11e01a486ab19 to your computer and use it in GitHub Desktop.
Save duvillierA/ff6b95a11e01a486ab19 to your computer and use it in GitHub Desktop.
Ember.js : Infinite scroll example
App.LocationRoute = Ember.Route.extend({
model: function() {
return this.store.find('location', params).then(function(result){
return result.get('content.firstObject');
});
}
});
App.LocationController = Ember.Controller.extend({
hasNoMoreContacts: false,
actions : {
loadMoreContacts: function() {
var self = this;
this.set('loading', true);
this.store.find('location', params).then(function(results) {
var contacts = results.get('content.firstObject.contacts');
if(!contacts.length) {
return self.set('hasNoMoreContacts', true);
}
self.set('loading', false);
self.get('contacts').pushObjects(contacts);
});
}
}
});
App.LocationVIew = Ember.View.extend({
didInsertElement: function() {
$window.on("scroll", this, this.didScroll);
},
willDestroyElement: function() {
$(window).off("scroll", this.didScroll);
},
didScroll: function() {
if(this.isScrolledToBottom() && !hasNoMoreContacts) {
this.get('controller').send('loadMoreContacts');
}
},
isScrolledToBottom : function() {
var distanceToTop = $(document).height() - $(window).height(),
top = $(document).scrollTop();
return top === distanceToTop;
},
hasNoMoreContacts : function() {
return this.get('controller.hasNoMoreContacts');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment