对于 Meteor.js 应用程序中的 Iron Router 路由,“window.onload”的真正等价物是什么?
What is a true equivalent of `window.onload` for an iron router route in a Meteor.js app?
我有一条铁线是这样定义的(注意onAfterAction
和initProfileComponent
)
var initProfileComponent = function () {
var elements = document.querySelectorAll('.sliderComponent');
//... do stuff and decorate the elements
}
Router.route('newProfile', {
path: '/new-profile/:_id',
onAfterAction: function () {
initProfileComponent();
},
data: function () {
return {profile: Profile.findOne({_id: this.params._id})};
}
});
函数initProfileComponent
希望使用由meteor/mustache发布机制创建的document.querySelectorAll
元素来查找,这就是在onAfterAction
中调用的原因,但显然它没有工作,因为当调用它时元素尚未呈现。
模板定义如下:
<template name="newProfile">
<div class="main-title new-profile">
new profile
</div>
{{#each profile.properties}}
{{> sliderComponent}}
{{/each}}
</template>
<template name="sliderComponent">
<div class="sliderComponent dragdealer">
<div class="handle">{{label}}</div>
</div>
</template>
除了 onAfterAction
对我的用例执行 "buritos" 之外,where/how 可以定义一个回调以确保模板已完全呈现。或者,我应该订阅什么?
理想情况下,这应该在您的模板中完成。在 Meteor 中,所有模板都有 onCreated
和 onRendered
回调。如果你使用 onRendered,你可以确定所有 DOM 元素已经存在。
Template.sliderComponent.onRendered(function(){
var elements = $('.sliderComponent');
//... do stuff and decorate the elements
});
像这样初始化模板是一种很好的做法。使用 Iron Router 参数化您的模板,就像您已经在做的那样。
我有一条铁线是这样定义的(注意onAfterAction
和initProfileComponent
)
var initProfileComponent = function () {
var elements = document.querySelectorAll('.sliderComponent');
//... do stuff and decorate the elements
}
Router.route('newProfile', {
path: '/new-profile/:_id',
onAfterAction: function () {
initProfileComponent();
},
data: function () {
return {profile: Profile.findOne({_id: this.params._id})};
}
});
函数initProfileComponent
希望使用由meteor/mustache发布机制创建的document.querySelectorAll
元素来查找,这就是在onAfterAction
中调用的原因,但显然它没有工作,因为当调用它时元素尚未呈现。
模板定义如下:
<template name="newProfile">
<div class="main-title new-profile">
new profile
</div>
{{#each profile.properties}}
{{> sliderComponent}}
{{/each}}
</template>
<template name="sliderComponent">
<div class="sliderComponent dragdealer">
<div class="handle">{{label}}</div>
</div>
</template>
除了 onAfterAction
对我的用例执行 "buritos" 之外,where/how 可以定义一个回调以确保模板已完全呈现。或者,我应该订阅什么?
理想情况下,这应该在您的模板中完成。在 Meteor 中,所有模板都有 onCreated
和 onRendered
回调。如果你使用 onRendered,你可以确定所有 DOM 元素已经存在。
Template.sliderComponent.onRendered(function(){
var elements = $('.sliderComponent');
//... do stuff and decorate the elements
});
像这样初始化模板是一种很好的做法。使用 Iron Router 参数化您的模板,就像您已经在做的那样。