Closed rclai closed 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.
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?
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.
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.
(BTW, I renamed destroy
to stop
.)
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
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.
Cool, thanks.
Can you elaborate on how to assign to Blaze Component? Do you mean doing this?
And will it clean itself up?