سؤال

I'm creating a Meteorite package that renders a table for a specific collection. I want this table to be customizable by adding optional custom columns as a templates:

<template name="myPluginTable">
  <table>
    <thead>
      <th>Column 1</th>
      <th>Column 2</th>
      {{> customHeaders}}
    </thead>
    <tbody>
      {{#each objects}}
        <tr>
          <td>{{firstValue}}</td>
          <td>{{secondValue}}</td>
          {{> customFields}}
        </tr>
      {{/each}}
    </tbody>
  </table>
</template>

Now this works fine as long as templates customHeaders and customFields are specified somewhere in the app.

But if these templates are omitted, an error is thrown. This cannot be circumvented by adding dummy templates to the package either, as trying to overwrite them raises an error as well.

The solution that I came up with:

Template.myPluginTable.helpers({
  customHeadersExist: function() {
    return typeof(Template.customHeaders) === "object";
    },
  customFieldsExist: function(user) {
    return typeof(Template.customFields) === "object";
  },
});

And in the templates:

{{#if customHeadersExist}}
  {{> customHeaders}}
{{/if}}

and

{{#if customFieldsExist}}
  {{> customFields}}
{{/if}}

Now this works, but the solution seems overtly complicated for such a simple task - do I really need to declare helper functions for each of the optional templates? So my question really is:

What is the best way to make a package customization template optional? Is it this, or is there a better way?

هل كانت مفيدة؟

المحلول

The easiest solution that comes to my mind is to implement a custom helper:

UI.registerHelper('safeRender', function () {
  var component = Template[this.name];
  if (component) {
    return component.extend({data: this.data});
  }
  return UI.Component;
});

which you can use in your templates like this:

{{> safeRender name='customHeaders' data=.}}

The data=. part is there to ensure that the template will be rendered in the same data context.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top