peerlibrary / meteor-computed-field

Reactively computed field for Meteor
https://atmospherejs.com/peerlibrary/computed-field
BSD 3-Clause "New" or "Revised" License
18 stars 6 forks source link

Assigning in Blaze Component #2

Closed rclai closed 9 years ago

rclai commented 9 years ago

Can you elaborate on how to assign to Blaze Component? Do you mean doing this?

BlazeComponent.extendComponent({
  onCreated: function () {
    this.totals = new ComputedField(function () {
      // My computations...
    });
  }
});

And will it clean itself up?

kostko commented 9 years ago

And will it clean itself up?

No, you have to call this.totals.destroy() to stop the autorun unless it is already called from a reactive computation in which case it will be automatically stopped.

rclai commented 9 years ago

So I would need to destroy them all onDestroyed in the Blaze Component?

Why not bake an internal method into Blaze Component that creates a ComputedField the way embox-value does in their package, that way it auto-cleans itself when the template is destroyed?

mitar commented 9 years ago

See example in the README how you can make automatic cleanup of all computed fields.

You could add this to your base class' onDestroyed which extends Blaze Component without any ill effect if there are no computed fields.

mitar commented 9 years ago

The issue is that I do not want to assume in this package that you do not want for the field to survive being working also after onDestroyed. In fact, I sometimes use a pattern where I am reusing the component and I am rendering the same component multiple times. Sometimes you might want things to be stopped in onDestroyed, but sometimes you do not want, because you want them to be kept propagated even when they are not rendered.

mitar commented 9 years ago

(BTW, I renamed destroy to stop.)

mitar commented 9 years ago

My biggest issue is that this code is using global Tracker.autorun and not component's one. So you cannot access template instance inside the autorun. :-( See https://github.com/meteor/meteor/issues/4494

mitar commented 9 years ago

OK, I improved the computed field so it is not necessary to stop it anymore manually. It will be stopped and cleaned automatically when it is not needed anymore. And restored if it is needed again. So just create the fields whenever and wherever you need them and this is it.

rclai commented 9 years ago

Cool, thanks.