Meteor.js: Avoiding Unnecessary Updating / Computing / Reprocessing Using Dot Notation

I need a way to avoid useless calculations; In reactive functions, I have a reactive object, I just need one property, but if any of this object changes, the whole function is recalculated:

Template.registerHelper('name', function() {
    console.log("running");
    return Meteor.user().profile.name;
});

(In html

<body><p>My name: {{name}}</p></body>

)

Now change your age:

Meteor.users.update({_id:Meteor.userId()},{$set:{"profile.age":20}});

You guess what you see on your console (second time ...)

running               x2

but it should only be started once, because the name has not changed

Why is it important? In my application, I have complex calculations, and the user status of online / inaction easily changes.

+4
source share
1 answer

. Meteor.user(), , , .

, , ( ) , , , . :

var prop = new ReactiveVar();

Template.template.onRendered(function() {
  this.autorun(function() {
    prop.set(Meteor.user().profile.name);
  });
});

Template.template.helpers({
  hlpr: function() {
    console.log("RERUN!!!");
    return prop.get();
  },
});
+1

All Articles