Viewmodel: possible for child value to still be bound to parent?

Hey guys, loving using viewmodel with Blaze.

I was trying to figure out if there was a way of abstracting my often reused form fields using a sub template that accepts properties as arguments (and still maintain the bindings). Example of the code I repeat for almost all my fields (using Jade/Pug)

.form-item.half($b="class: { has-error: firstName.invalid && dirty }")
  label First name
  input(type="text" $b="value: firstName")
  span.error($b="if: firstName.invalid && dirty, text: firstName.message")

I tried abstracting that into a reusable blaze template but the bindings don’t seem to update, eg:

+formItem label="First name" field=firstName dirty=dirty
template(name="formItem")
  .form-item.half($b="class: { has-error: field.invalid && dirty }")
    label {{ label }}
    input(type="text" $b="value: field")
    span.error($b="if: field.invalid && dirty, text: field.message")

Hi,

You should use ‘vmRef’ for that.

Info in:
https://viewmodelblaze.azurewebsites.net/docs/misc#controls

<template name="binding">
  First Name: {{> textBox text=(vmRef 'firstName') }}
  Last Name: {{> textBox text=(vmRef 'lastName') }}
  Hello from the parent: {{firstName}} {{lastName}}
</template>
1 Like

Brilliant, thanks so much!