theara
September 22, 2015, 10:36am
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}}
shock
September 22, 2015, 10:51am
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();
theara
September 22, 2015, 11:01am
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}}
shock
September 22, 2015, 11:42am
4
I already wrote how to pass handle name.
But there are no sub names.
Steve
September 22, 2015, 1:45pm
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>
2 Likes
theara
September 23, 2015, 1:11am
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?
mrzafod
September 23, 2015, 2:53am
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();
theara
September 23, 2015, 3:30am
8
Could I create global register helper for this?
(I don’t want to create on template…helpers, because do many time);
mrzafod
September 23, 2015, 3:33am
9
Template.registerHelper "subReady", (name) ->
Template.instance()[name]?.ready?()
theara
September 23, 2015, 3:57am
11
Could I pass the sub name
like array?
{{#if subReady "name1" "name2" ...}}
{{#if subReady}} // for all
Please example for template helper params.
ephemer
September 27, 2015, 10:50am
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);
});
ephemer
September 27, 2015, 10:55am
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