Closed arggh closed 7 years ago
It's "working as expected". The problem is the way Blaze works with helpers and child templates. By the time the parent calls the helper the child isn't available, so the reference is undefined. That means as far as Blaze is concerned there's nothing to rerun.
It works with the text binding because VM waits for children before binding the elements.
Here's what you can do instead to use handlebars:
Template.checkboxes.viewmodel({
isChecked: false, // May want to use a string since Blaze doesn't display false.
autorun() {
this.isChecked(this.checkbox2.checked());
}
});
I thought I had understood that part, which is why my helper was actually like this at first:
isChecked() {
if (this.checkbox2) return this.checkbox2.checked();
}
But then nothing would ever happen, which I don't understand. I thought I'd used this pattern before, but apparently not since it doesn't work.
What's puzzling me most about this is what's happening in the console when running the repro.
this
gives me the ViewModel, with already the property checkbox2
referencing the child VMthis.checkbox2
gives me undefined!In your first example the helper returns without ever running a reactive source. In the second example the console is playing tricks on you. It keeps a reference to the object so by the time you expand the object the child is already there.
On Aug 18, 2017 2:15 AM, "arggh" notifications@github.com wrote:
I thought I had understood that part, which is why my helper was actually like this at first:
isChecked() { if (this.checkbox2) return this.checkbox2.checked(); }
But then nothing would ever happen, which I don't understand. I thought I'd used this pattern before, but apparently not since it doesn't work.
What's puzzling me most about this is what's happening in the console when running the repro.
— You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub https://github.com/ManuelDeLeon/viewmodel/issues/293#issuecomment-323268984, or mute the thread https://github.com/notifications/unsubscribe-auth/AED31hY4ds4Y0r6y3Lh-mIeUnerBr-dOks5sZSvogaJpZM4O66Ej .
Ahh, of course. I've previously had other reactive sources in the same method, which is why they've seemed to work like I thought it would...
Thanks for helping me figure this out 👍🏻
When trying to access a child template's property by using a
ref
, it only works if binding via ViewModel binding. Trying to achieve the same via helper doesn't work, if the helper is used directly in the template.https://github.com/arggh/viewmodel-iframe-issue/tree/reference-issue
Not sure if this is supposed to work, but I'd expect to be able to show the checkboxes' checked-value via helper on the parent.
In the VM docs a similar scenario (a working one) is built in such way, that the child is accessed only by a manually triggered method call (button click), not by directly using a helper in the parent template code.