Can I send function as subscribe parameter


#1

This isn’t working. I get “object is not a function”.

//CREATE COLLECTION.
PlayersList = new Mongo.Collection('players');  

//SERVER.
if (Meteor.isServer) {

  //PUBLISH COLLECTION.
  Meteor.publish('PlayersPublished', function(paramFunc, filterIndex){
    var filter = paramFunc(filterIndex);
    return PlayersList.find(filter);
  });  

}

//CLIENT.
if (Meteor.isClient) {
  
  //PREPARE SUBSCRIBE PARAMETERS.
  function paramFunc(index) {  
    if (index==0) { return {                score : 10 }; }
    if (index==1) { return {name : "David", score : 10 }; }
  };  

  //SUBSCRIBE
  Meteor.subscribe('PlayersPublished', paramFunc, 0);

}

#2

Is these ideal?

In lib folder:
functionparam1 = function() {
  return PlayersList.find({score: 10});
}
functionparam2 = function() {
  return PlayersList.find({name : "David", score : 10 });
}

In server folder:
Meteor.publish('collection', function(index) {
   if(index==1){return functionparam1();}
   if(index==2){return functionparam2();}
...
});

In client folder:
...subscribe('collection','1')

#3

Since Functions are not JSON serializable, i guess you cannot send a function to the server.
Anyway, that would be a bad idea on a security point of view, if the server has some code to execute, it should come from the server, imho.


#4

Don’t try to pass a function, because you can’t. Put the logic from the function in the publication and just pass the index as a parameter. You can have multiple publications if you need and the client can subscribe to wher whichever it needs.