Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember.js rest api with pagination

At the moment I'm pointless how do achieve pagination with ember-data. I found out, that i can return 'meta' property in response and ember-data does not throw an error. But I just don't know how to access it, nor what is intended purpose of this property.

The few examples on internet assume that i have whole collection already loaded to ember, or they do little trick and do infinite scroll, which does'nt require information about page count.

I think that loading all records it's ok if I would have < 1k of them, but sometimes I'll be dealing with massive amounts of data (let's say apache logs). What then?

So basically I'm at the point in which I would like to use ember and ember-data to build my first real-life application, but I just think that it is not a good idea.

Ok, so anybody has any idea how to solve this basic, yet complicated, problem? :)

like image 465
Kamil Biela Avatar asked Mar 02 '13 16:03

Kamil Biela


1 Answers

Ok, so here are some ideas to get you started.

First, you have to start with a route and take an page number as a dynamic parameter.

this.resource('posts', { path: '/posts/:page' };

Then as I have no experience with Silex, you need to support some kind of server side parameters that could be used for pagination. For example offset and limit where first means how many records you want to skip and second how many record you want in select from there. Ideally you should implement them as query parameters like ?offset=0&limit=10.

Then you just implement your table route as follows:

App.TableRoute = Ember.Route.extend({
    model: function (params) {
        return App.Post.find({ offset: (params.page - 1) * 10, limit: 10  });
    }
});

You can then start doing some more magic and create your items per page parameter or validate the page number by fetching number of all records in advance.

like image 100
Myslik Avatar answered Nov 20 '22 00:11

Myslik