How to create helper of "subReady" in template?


#1

I have like this:

// Html
<template name="...">
    {{#if subReady}}
         data...
    {{else}}
        {{> loading}}
    {{/if}}
</template>

// JS
showTpl.onCreated(function () {
    var self = this;
    var data = self.data;
    self.handle = self.subscribe('sample_locationById', data._id);
});

showTpl.helpers({
    subReady: function () {
        var instance = Template.instance();
        return instance.handle.ready();
    },
    data: function () {
        var self = this;
        var data = Sample.Collection.Location.findOne(self._id);
        return data;
    }
});

I don’t want to create method on helpers({ subReady: ...}}.
I would like to create helper in Template

{{#if subReady "subNameOnCreated"}}
         data...
    {{else}}
        {{> loading}}
    {{/if}}

#2

I dont understand what is the problem.
That string in Template.subscribe is name of publication.
You can name your handle how you want.

Or accept argument in helper with for example handle name by something like this.
I did not test it.

 subReady: function (handle) {
        var instance = Template.instance();
        return instance[handle].ready();

#3

I create subReady for all my template, So I would to create the helper and then use by pass the name of sub

{{#if subReady "subNameOnCreated"}}
         data...
    {{else}}
        {{> loading}}
    {{/if}}

#4

I already wrote how to pass handle name.
But there are no sub names.


#5

There already is a built-in subReady handler, no need to create one:

Template.showTpl.onCreated(function () {
    this.subscribe('sample_locationById', this.data._id);
});
<template name="showTpl">
    {{#if Template.subscriptionsReady}}
         ...
    {{else}}
        {{> loading}}
    {{/if}}
</template>

#6

Thanks for all. But I have more then one sub

Template.showTpl.onCreated(function () {
   this.A =  this.subscribe('sample_locationById', this.data._id);
   this.B =  this.subscribe('sample_locationById2', this.data._id);
});

And then I would like to check by name?


#7

@theara, It is already answered by @shock :

Means

<template name="myView">
  {{#if subReady "A"}}...{{/if}}
</template>

And for the helper

return instance["A"].ready();

#8

Could I create global register helper for this?
(I don’t want to create on template…helpers, because do many time);


#9
Template.registerHelper "subReady", (name) ->
  Template.instance()[name]?.ready?()

#10

Thanks, I will try :smiley:


#11

Could I pass the sub name like array?

{{#if subReady "name1" "name2" ...}}

{{#if subReady}} // for all

Please example for template helper params.


#12

For an array of subscriptions and for all subscriptions at once, the helper will look more like this (I’m writing this on my iPhone so there may well be mistakes in the code):

Template.registerHelper('subsReady', function() {
  var tmpl = Template.instance();
  // get the list of subs we're interested in
  var subs = _.toArray(arguments);
  // if none listed use internal helper to check for ALL subs
  if (!subs.length) return tmpl.subscriptionsReady();

  // return true when all are ready
  var subReady = function (subName) {
    return tmpl[subName].ready();
  };
  return _.all(subs, subReady);
});

#13

Also, consider avoiding the variable name instance when referring to your template instance. I know that naming convention has been spread by some reputable literature (including Discover Meteor, from memory), but if you think about it, that name is about as meaningless as calling your var variable


#14

Look great :smiley:
Very Thanks.