Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

infinite scroll with ember.js (lazy loading)

I have a view where there can be a large number of items for the user to scroll through and I'd like to implement infinite scrolling to enable progressive loading of the content.

It looks like some folks have done pagination but Google doesn't bring up anyone discussing how they've done infinite lists with Ember/Ember Data. Anyone already worked through this and have a blog post/example code to share?

like image 583
outside2344 Avatar asked Aug 10 '12 18:08

outside2344


2 Answers

I've implemented an infinite scroll mechanism at the GitHub Dashboard project, I'm currently developing. The feature is added in commit 68d1728.

The basic idea is to have a LoadMoreView which invokes the loadMore method on the controller every time the view is visible on the current viewport. I'm using the jQuery plugin inview for this. It allows you to register for an inview event, which is fired when the element of the specified selector is visible on screen and when it disappears.

The controller also has properties which indicate whether there are more items to load and if there are currently items fetched. These properties are called canLoadMore and isLoading.

The LoadMoreView basically looks like this:

App.LoadMoreView = Ember.View.extend({   templateName: 'loadMore',   didInsertElement: function() {     var view = this;     this.$().bind('inview', function(event, isInView, visiblePartX, visiblePartY) {       if (isInView) Ember.tryInvoke(view.get('controller'), 'loadMore');     });   } }); 

where the loadMore template is defined as follows:

{{#if isLoading}}     fetching some more stuff <img width="10" src="img/ajax-loader.gif" > {{else}}     {{#if canLoadMore}}         <a {{action "loadMore" target="controller" }}>click to load more items</a>     {{else}}         <i>no more items</i>     {{/if}} {{/if}} 

The controller which handles the fetching of more items is then implemented as follows. Note that in the loadMore method a query on the store is performed, which loads a specific page of of entries for a model.

App.EventsController = Ember.ArrayController.extend({   currentPage: 1,    canLoadMore: function() {     // can we load more entries? In this example only 10 pages are possible to fetch ...     return this.get('currentPage') < 10;   }.property('currentPage'),    loadMore: function() {     if (this.get('canLoadMore')) {       this.set('isLoading', true);       var page = this.incrementProperty('currentPage');        // findQuery triggers somehing like /events?page=6 and this       // will load more models of type App.Event into the store       this.get('store').findQuery(App.Event, { page: page });     } else {       this.set('isLoading', false);     }   } }); 

The only thing left is to initially set the content of the controller to the result of a filter function, so the content is updated when new models are loaded into the store (which happens due to the findQuery method in the loadMore of the controller). Also, a query hash is added when the filter is invoked. This ensures that an initial query to the server is made.

App.eventsController = App.EventsController.create({     content: [] });  var events = App.store.filter(App.Event, { page: 1 }, function(data) {     // show all events; return false if a specific model - for example a specific     // type of event - shall not be included     return true; }); 
like image 158
pangratz Avatar answered Sep 26 '22 03:09

pangratz


Were you aware of the newly released Ember.ListView component?

https://github.com/emberjs/list-view

It was announced at the February San Francisco Ember Meetup. Here's a slidedeck from Erik Bryn, one of the Ember Core developers about using it:

http://talks.erikbryn.com/ember-list-view/

like image 37
commadelimited Avatar answered Sep 26 '22 03:09

commadelimited