Don't auto update data on template when the Collection updated (meteor-reactive-method)

I have with meteor-reactive-method:

// --------------Server
Meteor.methods({
   getData: function(){
      var data = Collection.Customer.find().fetch();
      return data;
   }
})

// ----------------Client
// Template
{{#each data}}
...............
{{/each}}

// Helper
data: function(){
   var data = ReativeMethod.calle('getData');
   return data;
}

Don’t auto update, when I update collection.
Please help me.

For me it seems that method is reactive other way - it reevaluate when you change it’s arguments.
Not that it would return output of collection.
And also this way using collection return to client would be very network heavy.

Use subscription, that is perfect usecase for it.

2 Likes

I use this way on my reports generator.
I do any calculations on the method before send the result to the client, so have any solution for this???
Thanks for your helping.

Well, you can still call method to update data and react on it’s callback by doing action you want.
Rendering template, or changing reactive variable and than using that variable in helper and template something like this pseudocode:

{{ #if variableIsTrue }}
something
{{else}}
something else
{{/if}}

etc…

Or do nothing and if you are subscribed to same set you will see changes reflected on client in real time.

I don’t understand, could example

Hi @theara, I’m guessing @shock means something like

Template.x.created = function() {
  var instance = this;
  instance.reactive = new ReactiveVar();
  instance.autorun() {
     Meteor.call('method', function(value) {
        instance.reactive.set(value);
     }
  }
}
Template.x.helpers({
  variableIsTrue: function() {
     return Template.instance().reactive.get();
  }
}
1 Like

Yes, something like that.

But to better help we would need to know usecase. What exactly you want to do after the method is finished.
If you need to subscribe, or what is the expected action.

Reports are normally run against a snapshot in time (end-of-day, end-of-month, etc). You do not need reactive data for this use case and a (static) method call is fine.

On the other hand, if you are wanting to display information as data changes, then you do need reactive data, and as @shock says, pub/sub is the way to go.

If you want a reactive, pub/sub model, which requires transformed data, then you should look at collection transforms and/or collection hooks. One or other of these may allow you to manipulate your collection results for your reporting, while still giving you reactive updates.

1 Like

Pub/sub is for collection, I don’t get data from pub directly.

I don’t understand - you said:

So I (we all) assumed you were getting data from a collection.

Could you example to use pub /sub to generate report like above

In lib/Collection.js

// This assumes "Collection" has already been defined as a global object (Collection = {})
// and is to conform with your own examples.
Collection.Customer = new Mongo.Collection('customer', {
  transform: function(doc) {
    ... extend doc here ...
    return doc;
  }
});

In server/report.js:

Meteor.publish('reportData', function() {
  return Collection.Customer.find();
});

In client/report.js:

Template.report.onCreated(function() {
  this.subscribe('reportData');
});

Template.report.helpers({
  data: return Collection.Customer.find();
});

In template/report.html:

{{#if Template.subscriptionsReady}}
  {{#each data}}
...
  {{/each}}
{{/if}}

[Edited for typo and to add in the collection with transform definition]

If you dont want reactivity, you can always use something like this
return YourPosts.find({},{reactive: false});

But my report (payment report) have conditions with many collections such as category, item, order, payment. So I need to pub/sub all of collectiona and then do …

You can aggregate the data and then provide it inside you publish method with this.added

Client:

Reports = new Mongo.Collection('reports', null) // null means only client side Collection
Meteor.subscribe('reports-publication', 'someId')

Server: inside a publish method

Meteor.publish('reports-publication', function(id) {
  var oneFoo = Foo.findOne(id);
  var someBars = Bars.find({fooId: oneFoo._id}).fetch();
  oneFoo.bars = someBars;

  // Here we inform the subscription to add oneFoo to the ClientSide Reports Collection
  // But it has a new field in it, bars!
  this.added('reports', oneFoo._id, oneFoo);
});

Please don’t use this in your app, read first more about Meteor publish:
http://docs.meteor.com/#/full/meteor_publish