1

我一直在尝试按照以下答案在 emberjs 中实现延迟加载:https ://stackoverflow.com/a/11925918/341358 。但是当 ember 加载初始数据集时,我被卡住了。出于某种原因,第一个 ajax 调用不断调用:/newsitems而不仅仅是第一页:/newsitems?page=1。从那时起,loadmore 功能就很好用,返回给我一个有限的数据集,用于第 2、3、4、...

所以我的问题是:如何确保只加载第一页的项目而不是一次加载所有项目?

这是我的路线的样子:

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

这是我的控制器:

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);
    }
  }
});
4

1 回答 1

1

您可以更改路线以包含默认页码1吗?

例如

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

编辑:如果您从控制器获取页码怎么办?

于 2013-03-06T19:54:04.090 回答