[英]What is the equivalent function in meteor js for window.onload(function(){}) and $(document).ready(function(){}) in jQuery?
[英]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
調用的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”我使用的情況下,這里的/一個人如何可以定義一個回調確保模板完全呈現。 或者,我應該訂閱什么?
理想情況下,這應該在您的模板中完成。 在 Meteor 中,所有模板都有onCreated
和onRendered
回調。 如果您使用 onRendered,您可以確保所有 DOM 元素都已經存在。
Template.sliderComponent.onRendered(function(){
var elements = $('.sliderComponent');
//... do stuff and decorate the elements
});
像這樣初始化模板是一種很好的做法。 正如您已經在做的那樣,使用 Iron Router 來參數化您的模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.