Reactive publishing without parameters

74 views
Skip to first unread message

Slobodan Blazeski

unread,
Feb 25, 2015, 9:03:42 AM2/25/15
to meteo...@googlegroups.com
I have a constant array (issues)  which subset I want to  publish.
Currently I'm doing something like below:

Issues= new Mongo.Collection("issues"); // Created just for the cursor, would prefer simple array

Meteor.reactivePublish('issues',function(){
     var simulation = Simulations.findOne({userId: this.userId}, {reactive:true});
      return Issues.find({date:{$lte: simulation.date}});      
});

Since data contained in the issues is const, and I don't want to use parameters as described in
the filtering information must come exclusively from the server.

Is there any way to avoid reactive publish and if possible creating unnecessary mongo collection ?


thanks
Slobodan






Jeremy Hall

unread,
Feb 25, 2015, 10:19:39 PM2/25/15
to meteo...@googlegroups.com
Could you please explain why you don't want to use parameters passed from the client? That Stack Overflow post is either out of date, or very misleading. If you are subscribing to your collection on the client from a code block that is reactive (such as a Tracker.autorun, or an ironrouter's controller functions such as waitOn or subscriptions), and the parameter you are passing into the subscription changes based on some reactive dependency, then the client's subscription will be updated based on the filter you've set in the data publication.

Slobodan Blazeski

unread,
Feb 26, 2015, 1:59:36 AM2/26/15
to meteo...@googlegroups.com
I don't want to send it from the client due to security reasons. Basically that parameter controls how far the user has progressed in the simulation, if they could send arbitrary date they'll be able to time travel in the future.

Slobodan Blazeski

unread,
Feb 28, 2015, 2:15:36 PM2/28/15
to meteo...@googlegroups.com
I've solved my problem without reactive join, though solution seems like a hack.
I'm observingChanges to the collection the serves as my filter, but I'm using the low level mechanism 
to return data from my list.
Notice the ugly self.maxBookYear. I don't know how to access Subscription documents to see that latest year.



// Client 
Books = new Meteor.Collection('books');

// Server
Observations = new Meteor.Collection('observations');

var year = 1999;

Meteor.startup(function () {
Observations.remove({});
Observations.insert({
year: year
});
});
// Some very interesting books
var books = [
{title: 'Book 1', year: 1999},
{title: 'Book 2', year: 2000},
{title: 'Book 3', year: 2001},
{title: 'Book 4', year: 2002},
{title: 'Book 5', year: 2003},
{title: 'Book 6', year: 2004},
{title: 'Book 7', year: 2005},
{title: 'Book 8', year: 2006},
{title: 'Book 9', year: 2007}
];


Meteor.publish('books', function () {
var self = this;
var initializing = true;

var handle = Observations.find({}, {sort: {year: -1}, limit: 1}).observeChanges({
added: function (id, fields) {
console.log('added');
if (!initializing) {
var addBooks = _.filter(books,function(book){
return self.maxBookYear < book.year && book.year <= fields.year;
});
self.maxBookYear = fields.year > self.maxBookYear ? fields.year : self.maxBookYear;
_.each(addBooks, function (book) {
var id= Random.id();
self.added('books', id, book);
});
}
}
});
initializing = false;
self.maxBookYear = year;
self.added("books", Random.id(), {title: 'Book 0', year: year});
self.ready();
});


Meteor.setInterval(function () {
console.log('insert');
Observations.insert({
year: ++year
});
}, 10000);
Reply all
Reply to author
Forward
0 new messages