Refactoring templates - parent problem


#1

We have a number of modal dialogs. The Cancel/Save part of them is identical, so it would be good to factor that out to its own template.

But each dialog has a reactive variable on the template itself, controlling the active state of the Save button. There doesn’t seem to be any sane way to give the sub-template code access to this variable, making this refactoring impossible. Or am I missing something?

I’m primarily interested in if refactoring like this is doable in Meteor, but other ways of making this work are also interesting.

Concrete code:

<template name="modalA">
  <div id="modalA" class="modal fade">
    // content omitted
    {{> cancelSave}}
  </div>
</template>

Template.modalA.created = function() {
  this.isSaveableReact = new ReactiveVar(false);
    template.autorun(function() {
    template.isSaveableReact.set(!!Template.currentData());
  });
};

Template.cancelSave.helpers({
  isSaveable: function() {
    return Template.instance().parent().isSaveableReact.get(); // THIS IS IMPOSSIBLE
  }
}

#2

Use this:

// Returns an ancestor instance of this template instance (by name)
// See
// https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/meteor-core/H9XUndnLpf0/N_hp69p9SsEJ
Blaze.TemplateInstance.prototype.parentInstance = function (templateName) {
  if (! /^Template\./.test(templateName))
    templateName = 'Template.' + templateName;
  var view = this.view;
  while (view = view.parentView)
    if (view.name === templateName)
      return view.templateInstance();
};

Or have a look at https://github.com/aldeed/meteor-template-extension.


#3

The latest version of meteor-template-extensions throws warning because it tries to create template functions that already exist in the latest version of Blaze that comes with METEOR@1.1.0.2

Template.onCreated = function (callback) {
  globalHooks.created.push(callback);
};

Template.onRendered = function (callback) {
  globalHooks.rendered.push(callback);
};

Template.onDestroyed = function (callback) {
  globalHooks.destroyed.push(callback);
};