Answer the question
In order to leave comments, you need to log in
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
}
});
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
});
}
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question