1

I've been trying to implement lazy loading in emberjs by following this answer: https://stackoverflow.com/a/11925918/341358 . But I'm stuck when ember loads the initial dataset. For some reason the first ajax call keeps calling: /newsitems instead of only the first page: /newsitems?page=1. From then on, the loadmore functionality works great, returning me a limited data set for page 2, 3, 4, ...

So my question is: how do I make sure that only the items for the first page are loaded and not all of them at once?

Here's how my route looks like:

App.NewsitemsRoute = Ember.Route.extend({
  model: function() {
    return App.Newsitem.find();
  }
});

Here's my controller:

App.NewsitemsController = Ember.ArrayController.extend({
  currentPage: 1,

  canLoadMore: function() {
    return this.get('currentPage') < 10;
  }.property('currentPage'),

  loadMore: function() {
    if (this.get('canLoadMore')) {
      this.set('isLoading', true);
      var page = this.incrementProperty('currentPage');

      this.get('store').findQuery(App.Newsitem, {page:page});
    }
    else
    {
      this.set('isLoading', false);
    }
  }
});
Community
  • 1
  • 1
polyclick
  • 2,554
  • 3
  • 30
  • 52

1 Answers1

1

Can you change your route to include a default page number of 1?

e.g.

App.NewsitemsRoute = Ember.Route.extend({
  model: function() {
    var controller = this.controllerFor('Newsitems');
    return App.Newsitem.find({page: controller.get('currentPage')});
  }
});

Edit: What if you get the page number from the controller?

Ben
  • 9,542
  • 5
  • 39
  • 41
  • Great suggestion but I already tried this before. This makes sure the first call to the backend is: `/newsitems?page=1` but calls for next pages doesn't update the view. I can see that a call to the backend is made like: `/newsitems?page=2` but the view keeps on showing the first few items. Any thoughts what might cause this? – polyclick Mar 07 '13 at 10:50
  • This seems redundant since you're already initiating a `findQuery` from inside your controller, but what happens when you get the page number from the controller as I've done above? – Ben Mar 07 '13 at 16:11
  • The same happens as before: the first time /newsitems?page=1 is called and the second time: /newsitems?page=2. Which is great! But, my view isn't updated, so the new records are never shown on the page. – polyclick Mar 07 '13 at 17:12