Declare "this.subscribe" in "onCreated" vs "onRendered"?

Declare “this.subscribe” in “onCreated” vs “onRendered”

showTpl.onCreated(function () {
    this.subProvince = this.subscribe('loan_provinceById', this.data._id);
});
showTpl.onRendered(function () {
    this.subProvince = this.subscribe('loan_provinceById', this.data._id);
});

Which I should?

Both will work as i’m sure you have proved already, but think about the lifecycle of the page… OnRendered happens (as the name suggests) once all the Dom elements associated with your template have been rendered, so if you wait until this point to subscribe you are going to provide a slightly worse user experience to your users.

OnCreated is the place to do template level subscriptions as described in the Meteor Guide

Thanks for your reply.
I have the function to check sub ready by name

/**
 * Template sub ready
 */
templateSubReady = function () {
    var subs, ready;
    var tmpl = Template.instance();
    var slice = [].slice;

    subs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
    if (subs.length === 0) {
        ready = tmpl.subscriptionsReady();
    } else { // > 0
        subs = subs.slice(0, subs.length - 1);

        console.log('TemplateSubReady: ');
        ready = _.reduce(subs, function (memo, sub) {
            var subReady = ((ref = tmpl[sub]) != null ? typeof ref.ready === "function" ? ref.ready() : void 0 : void 0);
            return memo && subReady;
        }, true);
    }

    console.log(ready);
    return ready;
};

Template.registerHelper('templateSubReady', templateSubReady);

For example:

<!--Show-->
<template name="loan_provinceShow">
    <!--Customer template sub ready by name-->
    {{#if templateSubReady "subProvince"}}
        {{#with data}}
            <dl class="dl-horizontal">
                <dt>ID:</dt>
                <dd>{{_id}}</dd>
                <dt>Kh Name:</dt>
                <dd>{{khName}}</dd>
                <dt>En Name:</dt>
                <dd>{{enName}}</dd>
            </dl>
        {{/with}}
    {{else}}
        {{> loading}}
    {{/if}}
</template>


//showTpl.onCreated(function () {
    //this.subProvince = this.subscribe('loan_provinceById', this.data._id);
//});
showTpl.onRendered(function () {
     this.subProvince = this.subscribe('loan_provinceById', this.data._id);
});

showTpl.helpers({
    data: function () {
        var data = Loan.Collection.Province.findOne(this._id);
        return data;
    }
});

1- Set sub on onCreated() work fine.
2- Set sub on onRendered() don’t work.
Please help me.

Given that the answer to your initial question was “use onCreated”, what is the issue your now trying to solve?

But again, if you think about the lifecycle of the page you will see why your global helper doesn’t work with subs that are created in the onRendered callback, by then the helper has been called once and you return tmpl.subscriptionsReady() which will return true (because no subscriptions exist yet).

Just use onCreated with this pattern (as per the Meteor Guide)

Thanks again, Now I use in onCreated.