Dynamic paging using ember

We are using EmberJs to build our webapp. I have pagination code that displays 5 items per 100 items. All thousand 100 elements are taken from one API call and will be passed to the "model" as an array ; and the controller will slice the array based on the page and page size.

I want to make a dynamic structure, which means I have to send an API call to the server with page inputs and page size inputs and get a specific response for every next page click.

Here's my code.

Router:

App.AccountPointsRoute = Ember.Route.extend({
    totalTransactions: null,
    model: function(params) {
        var page = this.controllerFor('accountPoints').get('page');
        var pointsURL= App.config.getEndpoint()+'/account/points';
        var getUserDetailsForViewUserAccount = function(){
            if(params.email){
                return Ember.$.getJSON(App.config.getEndpoint()+'/admin/userInfoByEmail?email='+params.email).then(function(data){
                    return data.payload.user.totalPoints;
                })
            }
        }
        var transactionURL = App.config.getEndpoint()+'/account/transactions?page='+page+'&pageSize=2&raceId=0&selectedDate='+ App.current_month() +'%20'+App.current_year();
        if(params.email){
             pointsURL = App.config.getEndpoint()+'/admin/points?email='+params.email;
             transactionURL = App.config.getEndpoint()+'/admin/transactions?page=1&pageSize=100&raceId=0&selectedDate='+ App.current_month() +'%20'+App.current_year()+'&email='+params.email;
        }
        var self = this;
        return Em.RSVP.hash({
            points:  Ember.$.getJSON(pointsURL).then(function(data){
                return data.payload;
            }),
            transactions: Ember.$.getJSON(transactionURL).then(function(data){
                self.set('totalTransactions', data.payload.totalRecords)
                return data.payload.logs;
            }),
            userPoints:getUserDetailsForViewUserAccount(),
        });
    },
    setupController: function(controller, models) {
        controller.set('points', models.points);
        controller.set('transactions', models.transactions);
        controller.set('userPoints',models.userPoints);
        controller.set('model', models.transactions);
        controller.set('totalRecords', this.get('totalTransactions'));
    }
});

      

And the controller for that

App.AccountPointsController = App.PaginationController.extend({
    needs:['application'],
    queryParams:['email', 'page'],
    page : 1,
    pageSize: 5,
    numPages: function () {
        var pageSize = this.get('pageSize'),
            //l = this.get('length');
            l = this.get('totalRecords');
        console.log("numPages "+ Math.ceil(l / pageSize));
        return Math.ceil(l / pageSize);
    }.property('pageSize','length'),

    paged: function() {
        var page = this.get('page');
        var transactions = this.get('model');
        var page = this.get('page') - 1,
        pageSize = this.get('pageSize'),
        start = page * pageSize,
        end = start + pageSize
        length = transactions.get('length');
        transactions = transactions.slice(start,end);
        this.set('length', length);
        return transactions;
    }.property('email', 'page', 'model', 'pageSize','pages'),

}); 

      

What do I need for the code to call the API call inside the "paged" method to get a specific response to the page number from the server. I am looking forward to your reply.

+3


source to share


1 answer


You don't want to call the API call from your "paged" method, because it's in the controller. Your route should take care of API calls to retrieve data for your model.

You want your links in your template to include query parameters specifying the desired page and page type. So something like:

<div class ="tableNavigation">
    {{#link-to 'accounts' (query-params page=previousPage pagesize=currentPageSize)}}Previous Page{{/link-to}}
    {{#link-to 'accounts' (query-params page=nextPage pagesize=currentPageSize)}}Next Page{{/link-to}}
    Current Page: {{page}}
    Next: {{nextPage}}
    Prev: {{previousPage}}
</div>

      

Then, in your route, include params.nextPage and params.currentPageSize in the query strings for your AJAX calls. Note that changing the request parameters will usually not trigger another API call, so include in your route as well:



queryParams: {
        pagesize: {
            refreshModel: true
        },
        page: {
            refreshModel: true
        }
    },

      

Then, in your controller, enable queryParams: ['page', 'pagesize']

and execute functions like this, but with bounds checking:

nextPage: function() {
        return (parseInt(this.get('page')) + 1);
}.property('page'),

previousPage: function() {
        return (parseInt(this.get('page')) - 1);
}.property('page'),

      

+2


source







All Articles