How to respond effectively to a function in a meteor?

I have a function that does some gui logic and I need this to run every time the meteorite updates the template reactively.

I tried to put the code in Template.myTemplate.helpers as shown below, but then nothing works at all.

Template.ResourceManager.helpers({
        names : function(){
                myFunction();
                return resources.findOne({age : 20}).names;
    }
});

      

Basically, I need myFunction () to run every time something changes in resources. I cannot find a way to do this. I've tried looking into autoRun along with cursor.dependency, but I really don't understand how they work, or how to apply them here. Any help would be greatly appreciated! Thank!

+3


source to share


1 answer


You can try using cursor.observeChanges

in collection resources

.

function myFunction(id, fields){
  console.log("something happened on resources", id);
}

var cursor = resources.find();
cursor.observeChanges({
  added:myFunction,
  changed:myFunction,
  removed:myFunction
});

      



https://docs.meteor.com/#/full/observe_changes

+1


source







All Articles