Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right pattern for returning pagination data with the ember-data RESTAdapter?

I'm displaying a list of articles in a page that are fetched using the Ember Data RESTAdapter. I need to implement a bootstrap'esque paginator (see: http://twitter.github.com/bootstrap/components.html#pagination) and cant seem to find a sane pattern for returning pagination data such as, page count, article count, current page, within a single request.

For example, I'd like the API to return something like:

{
  articles: [{...}, {...}],
  page: 3,
  article_count: 4525,
  per_page: 20
}

One idea was to add an App.Paginator DS.Model so the response could look like:

{
  articles: [{...}, {...}],
  paginator: {
    page: 3,
    article_count: 4525,
    per_page: 20
  }
}

But this seems like overkill to hack together for something so trivial. Has anyone solved this problem or found a particular pattern they like? Is there a simple way to manage the RESTAdapter mappings to account for scenarios such as this?

like image 444
Tyler Love Avatar asked Jan 16 '13 04:01

Tyler Love


1 Answers

Try to use Ember Pagination Support Mixin and provide your own implementation of the following method. Instead of loading all the content, you can fetch the required content when the user is navigating the pages. All what you need initially is the total account of your records.

didRequestRange: function(rangeStart, rangeStop) {
    var content = this.get('fullContent').slice(rangeStart, rangeStop);
    this.replace(0, this.get('length'), content);
  }
like image 125
ken Avatar answered Oct 05 '22 23:10

ken