P
P
Pavel2015-03-25 05:41:58
MongoDB
Pavel, 2015-03-25 05:41:58

Filter and pagination in meteor js?

Dear, help me deal with the problem of
Meteor js.
For pagination of collections I use alethes:meteor-pages
The problem is the following:
In pagination, in auth I set a filter (on the server), if the user is a manager, publish only the clients assigned to him.

UsersPagination = new Meteor.Pagination(Meteor.users, {
    ...
    auth: function(skip, sub){
        //use alanning:roles
        if(Roles.userIsInRole(sub.userId, ['admin'])) { // see all users
            return Meteor.users.find({roles: 'user', user_status: {$in: [1,2,3,4,5]}}, {fields: {services: 0}});
        } else if(Roles.userIsInRole(sub.userId, ['manager'])) { // see own users
            return Meteor.users.find({manager_id: sub.userId, roles: 'user', user_status: {$in: [1,2,3,4,5]}}, {fields: {services: 0}});
        } else {
            console.log('whaa?');
            return false ;
        }
    },
    availableSettings: {
        filters: true,
        settings: true
    }
});

Users are shown, everything is fine.
Further, the template has a select with a filter by user status:
Template.usersPaginate.events({
   'change #user_status': function(event, template){
       var status_id = parseInt(event.currentTarget.value, 10) ;
       var filter = {} ;
       if(status_id !== 0) {
           filter.user_status = status_id ;
       }
       //....
       UsersPagination.set({
           filters: filter
       });
   }
});

When the filter is changed, the template is updated (a spinner appears), i.e. you can see that the data is being updated, BUT the filter that is installed on the client does not work, i.e. displays users with all statuses ... wtf ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2015-06-30
@Shutik

Subscribers, I solved the issue in the following way:

auth: function(skip, sub){
        if (!sub.userId) { return false; }
        var _serverFilter = {roles: 'user'};
        if(Roles.userIsInRole(sub.userId, ['admin'])) {
             //....
        } else if(Roles.userIsInRole(sub.userId, ['manager'])) {
            _serverFilter.manager_id = sub.userId ;
        } else {
            console.log('whaa?');
            return false ;
        }

        var userSettings = UsersPagination.userSettings[sub._session.id] || {};
        var uFilters = userSettings.filters || this.filters;
        var uFields = userSettings.fields || this.fields;
        var uSort = userSettings.sort || this.sort;
        var uPerPage = userSettings.perPage || this.perPage;
        var _filters = _.extend({}, uFilters, _serverFilter);
        var _options = { fields: uFields, sort: uSort, limit: uPerPage, skip: skip };
        return [ _filters, _options ];
    },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question