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
  .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")


You should use ‘vmRef’ for that.

Info in:

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

Brilliant, thanks so much!