Reactive publishing without parameters

79 vues
Accéder directement au premier message non lu

Slobodan Blazeski

non lue,
25 févr. 2015, 09:03:4225/02/2015
à 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

non lue,
25 févr. 2015, 22:19:3925/02/2015
à 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

non lue,
26 févr. 2015, 01:59:3626/02/2015
à 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

non lue,
28 févr. 2015, 14:15:3628/02/2015
à 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);
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message