Template.subscriptionsReady禁用onRendered功能 [英] Template.subscriptionsReady disables onRendered functionality

查看:96
本文介绍了Template.subscriptionsReady禁用onRendered功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Template.templatename.OnRendered中初始化日期选择器和表单验证.

I am initializing datepickers and form validation in Template.templatename.OnRendered.

 $('#dateAccepted').datepicker({
    endDate: new Date(),
    todayBtn: true,
    todayHighlight: true,
    autoclose: true
});

我正在Template.templatename.onCreated中进行订阅,集合查询等.

I am doing subscriptions, collection queries etc in Template.templatename.onCreated.

Template.insertContract.onCreated(function () {
var self = this;
self.autorun(function() {
    self.subscribe('getContracts');
    self.subscribe('getSuppliers' ,function(){
        var suppliers = Supplier.find({}).fetch();
        var sOptions = createOptions('supplier','supplierName',suppliers, true);
        Session.set('supplierOptions', sOptions);
    });
    self.subscribe('getItems');
});

});

如果我将Template.subscriptionsReady放在模板上,则日期选择器和表单验证将停止工作.没有错误消息.

If I put Template.subscriptionsReady on my template, datepickers and form validations stop working. No error messages.

推荐答案

这个问题经常被问到,但是诚然很难搜索. 是一个有关渲染轮播项目的类似问题.

This question gets asked a lot, but admittedly it's hard to search for. This is a similiar question about rendering carousel items.

正在发生的事情:

  1. 您的模板呈现并onRendered被调用.
  2. 您的订阅尚未准备好,因此日期选择器未呈现,但是初始化代码就像在其中一样在(1)中运行.
  3. 最终,日期选择器被渲染.
  1. Your template renders and onRendered gets called.
  2. Your subscription isn't ready yet, so the datepicker isn't rendered but the initialization code runs in (1) as if it had.
  3. Eventually the datepicker is rendered.

一个可能的答案是在子模板中呈现日期选择器.在子模板的onRendered中,您可以运行初始化代码,因为可以确保它位于DOM中.有关示例,请参见上述问题的答案.

One possible answer is to render the datepicker in a sub-template. In the sub-template's onRendered you can run the initialization code, as you can be guaranteed that it's in the DOM. See the answer to the aforementioned question for an example.

这篇关于Template.subscriptionsReady禁用onRendered功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆