Skip to content

Instantly share code, notes, and snippets.

@bazzel
Created February 9, 2014 21:06
Show Gist options
  • Save bazzel/8905925 to your computer and use it in GitHub Desktop.
Save bazzel/8905925 to your computer and use it in GitHub Desktop.
Infinite scroll with Ember.js and Ruby on Rails - These Ember Mixins are used by my Rails template for setting up infinite scrolling with Ember.js and Ruby on Rails. The template is located here https://github.com/bazzel/rails-templates/blob/master/ember-infinite-scroll.rb
InfiniteScroll =
ControllerMixin: Ember.Mixin.create(
hasMore: (->
@meta('page') < @meta('total_pages')
).property('@each')
meta: (key) ->
@store.metadataFor(@_type())[key]
loadMore: ->
if @get('hasMore')
@set('isLoading', yes)
@store.find(@_type(),
page: @meta('page') + 1
).then(=>
@set('isLoading', no)
)
_type: ->
fullClassName = @get('model').type.toString()
className = fullClassName.replace(/.*\./, "")
className.underscore()
),
ViewMixin: Ember.Mixin.create(
startObserveScrolling: (->
$(window).bind('scroll', => @didScroll())
).on('didInsertElement')
stopObserveScrolling: (->
$(window).unbind('scroll')
).on('willDestroyElement')
didScroll: ->
if @isScrolledToBottom()
@get('controller').loadMore()
isScrolledToBottom: ->
distanceToTop = $(document).height() - $(window).height()
top = $(document).scrollTop()
top is distanceToTop
)
window.InfiniteScroll = InfiniteScroll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment