繁体   English   中英

在完成DOM之前调用渲染 - 流星火焰

[英]rendered called before DOM completion - meteor blaze

我正在使用json创建一个动态表单,并尝试使用jquery-validation插件将验证规则添加到输入字段。

Json结构和辅助方法是:

var fields = [{
    label: "Name",
    type: {name: 'STRING'},
    validationRules: {
        required: true,
        maxlength: 100,
        minlength: 3
    }
},{
    label: "Currency",
    type: {name: 'CHECKBOX'},
    defaultValue: ['USD', 'INR'],
    validationRules: {
        required: true
    },
    sourceCollection: 'oNLFfi4L3zgNLhScv',
}] ;

Template.eventCreate.helpers({
    fields: function(){
        console.log("calling fields");
        fields.forEach(function(field, index){
            field.sourceCollectionData = StaticLists.find({_id: field.sourceCollection});
        });
        return fields;
    }
});

模板看起来像:

<template name="eventCreate">
    <form id="newEventForm" class="form-horizontal">
        {{#each fields}}
            <div class="form-group">
                <label class="col-xs-2 control-label">{{label}}</label>
                <div class="col-xs-6">
                    {{#if equals type.name 'STRING'}}
                        <input name="{{label}}" id="{{label}}" class="form-control" placeholder="Enter {{label}}" value="{{defaultValue}}" />
                    {{/if}}
                    {{#if equals type.name 'CHECKBOX'}}
                        {{#each sourceCollectionData}}
                            {{#if isActive}}
                                <div class="col-xs-2 checkbox">
                                    <label class="checkbox-inline">
                                        <input type="checkbox" name="{{../label}}" id="{{../label}}" value="{{name}}" {{checked ../defaultValue}}> {{name}}
                                    </label>
                                </div> 
                            {{/if}}                               
                        {{/each}}                         
                    {{/if}}
                </div>               
            </div>
        {{/each}}
    </form>
</template>

现在我尝试在渲染方法中添加验证规则:

Template.eventCreate.rendered = function(){
    $('#newEventForm').validate({
       ....
    });

    fields.forEach(function(field, index){
        if(field.validationRules){
            $('#'+field.label).rules('add', field.validationRules);
        }
    });
}

它适用于输入文本但会抛出复选框的异常,因为复选框DOM仍然没有布局,并且没有ID为“Currency”的元素。

我假设在流星火焰渲染中仅在DOM渲染完成时调用一次。 虽然它在这里被调用一次但在DOM渲染完成之前。

编辑

之前我在js文件中对JSON进行了硬编码,现在我正在从mongo中检索它。

但它似乎只是第一次调用渲染回调而且每次mongo集合更改时都不会调用(这反过来会更新DOM)

Template.eventCreate.helpers({
    fields: function(){
        var template = EventTemplate.findOne({name: 'Job Template'});
        console.log("template", template);
        if(template){
            Session.set("template", template);
            template.fields.forEach(function(field, index){
               field.sourceCollectionData = StaticLists.find({_id: field.sourceCollection});
            });
            return template.fields;
        }        
    }
});

Template.eventCreate.rendered = function(){
    $('#newEventForm').validate({
        ...
        ...
    console.log("rendering main template");

   addValidationRules(Session.get('template'));
}

现在控制台输出类似于:

template undefined event_create.js?bfeba9389401dac358bc313bd9d4205818e356a1:52
rendering main template event_create.js?bfeba9389401dac358bc313bd9d4205818e356a1:98
template Object {_id: "iFDndmjavtFN8AdGQ", name: "Job Template", description: "Job Template",     fields: Array[13]}

这表明(我甚至尝试使用js脚本中的断点)当脚本加载模板未定义且前端没有呈现输入字段但调用了渲染回调时。 现在,稍后当模板填充数据时,将呈现输入字段,但不会再次调用回调。

建议的模式是把你的{{#each}}块的内容在不同的模板和挖掘这些模板的渲染事件。

顺便说一句,不再需要Meteor.defer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM