[英]Template.myTemplate.rendered not rendered until after a page refresh
To avoid the error "Exception in template helper: TypeError: Cannot read property 'profile' of undefined" I check that Meteor.user() has returned an object: 为了避免错误“模板帮助器中的异常:TypeError:无法读取未定义的属性'profile'”,我检查Meteor.user()是否返回了一个对象:
Template.create.isActive = function () {
var user = Meteor.user();
return user && user.profle.isActive;
};
When I implement this change it solves the error message, but creates a new problem... the 'Template.create.rendered = function ()' no longer gets rendered. 当我实现此更改时,它解决了错误消息,但创建了一个新问题……“ Template.create.rendered = function()”不再呈现。 It is only rendered after a refresh of the page. 它仅在刷新页面后呈现。 Any idea how to resolve this? 任何想法如何解决这个问题?
Code in Template.myTemplate.rendered... Template.myTemplate.rendered中的代码...
Template.create.rendered = function() {
if (!this._rendered) {
this._rendered = true;
// Form validation
$("#create").bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
/* submitButtons: '#fakeSubmit', Trick validator until https://github.com/nghuuphuoc/bootstrapvalidator/pull/244 is fixed */
submitHandler: function(validator, form, submitButton) {
var $form = $('#buildingCreate');
var building = {
company: $form.find('[name=company]').val(),
address: $form.find('[name=address]').val(),
floor: $form.find('[name=floor]').val()
};
Meteor.call('ensureBuilding', building.company, building.address, building.floor);
Router.go('create');
},
fields: {
company: {
trigger: 'blur',
validators: {
notEmpty: {
message: 'Please provide the company name'
}
}
},
address: {
trigger: 'blur',
validators: {
notEmpty: {
message: 'Please provide the company address'
}
}
},
floor: {
trigger: 'blur',
validators: {
notEmpty: {
message: 'Please provide the floor'
}
}
}
}
})
}
};
From the rendered docs: 从呈现的文档中:
This callback is called once when an instance of Template.myTemplate is rendered into DOM nodes and put into the document for the first time. 当Template.myTemplate的一个实例呈现到DOM节点中并首次放入文档时,将调用一次此回调。
If #create
is inside of an {{#if}}
block that is false when the template renders, validation will not get initialized. 如果在模板渲染时#create
位于{{#if}}
块的内部,该块为false,则不会初始化验证。
Change 更改
<template name="parent">
{{> create}}
</template>
<template name="create">
{{#if isActive}}
<div id="create"> ... </div>
{{/if}}
</template>
to 至
<template name="parent">
{{#if isActive}}
{{> create}}
{{/if}}
</template>
<template name="create">
<div id="create"> ... </div>
</template>
This way, #create
is sure to be in the DOM when rendered
is called. 这样,在调用rendered
时, #create
一定会在DOM中。 Also, given that rendered is only called once, you shouldn't need if (!this._rendered) ...
. 另外,由于render仅被调用一次,因此您不需要if (!this._rendered) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.