vijayst
vijayst

Reputation: 21866

Re-render Blaze template on text change

I have a Meteor Blaze Template based on autoform.

<template name="patientForm">

<div class='mdl-cell mdl-cell--12-col'>
    {{#autoForm id="insertUpdatePatientForm" collection="Patients" doc=selectedPatientDoc
    type=formType validation="browser" template="semanticUI"}}
    <div class='two fields'>
    {{> afQuickField name="firstName"}}
    {{> afQuickField name="lastName"}}
    </div>
    <div class='two fields'>
   {{> afQuickField name="phn.type"}}
   {{> afQuickField name="phn.value" class="ramq"}}
   </div>
       <div class='two fields'>
        {{> afQuickField name="birthDate"}}
        {{> afQuickField name="gender"}}
       </div>

    <button class="ui submit button" type="submit">Save</button>
    <div class="ui error message"></div>
    {{/autoForm}}
</div>
</template>

I want to handle the text change event for input with name phn.value. Based on the text, I want to auto-populate two other fields: gender and date of birth. I am doing it by changing the template data directly as follows:

Template.patientForm.events({
    'change .ramq': function changeRAMQ(event, templateInstance) {
        const { patient } = templateInstance.data;
        if (patient.phn.type === 'RAMQ') {
            const ramq = event.target.value;
            const yy = parseInt(ramq.substr(4, 2), 10);
            let mm = parseInt(ramq.substr(6, 2), 10);
            const dd = parseInt(ramq.substr(8, 2), 10);
            patient.gender = mm < 13 ? 'Male' : 'Female';
            if (mm > 50) {
                mm -= 50;
            }
            patient.birthDate = moment(new Date(yy, mm, dd)).format('YYYY-MM-DD');
        }
    },
});

I am getting the template data and directly modifying the gender and birthdate when the phn.value changes. However, the modified gender and birthdate does not re-render in the autoform / blaze template. Any way by which I can force re-render of Blaze template or alternate ways to effect changes to other controls in Blaze template?

Upvotes: 1

Views: 1114

Answers (2)

Xander
Xander

Reputation: 46

To enable reactivity and thus the re-rendering of the fields you should use a ReactiveVar (or ReactiveDict)

You can do this like this:

Template.patientForm.onCreated(function(){
  const instance = this;
  instance.birthDate = new ReactiveVar()
});

And in your helpers and events you can use instance.birthDate.set() / get()

Template.patientForm.helpers({
   birthDate() {
      return Template.instance().birthDate.get()
   }
});

Template.patientForm.events({
   'click something'(event, instance){
   ....
     instance.birthDate.set(value);
   ....
   }
});

Upvotes: 1

Christian Fritz
Christian Fritz

Reputation: 21374

You can't modify the template data directly (you can, but that's not reactive and will be overwritten). Where are you getting the template data from? A collection? a reactive variable? If so, modify the data there -- Blaze will notice the change and re-render.

Supposedly something like this will work:

Patients.update(templateInstance.data._id, {$set: {
  birthDate: ..,
  gender: .. 
}});

Upvotes: 2

Related Questions