简体   繁体   English

meteor.js的模板onload事件

[英]Template onload event for meteor.js

I know meteor exposes events such as "click", for DOM element but I'm wondering if there's a load event that's fired when a template or partial is loaded? 我知道meteor暴露了诸如“click”之类的事件,对于DOM元素,但是我想知道在加载模板或部分时是否有一个加载事件被触发了? How would I accomplish this? 我怎么做到这一点?

Thanks. 谢谢。

For Meteor starting from 0.4.0 preview, you can use Template.myTemplate.created . 对于从0.4.0预览开始的Meteor,您可以使用Template.myTemplate.created

In Template.myTemplate.created the DOM is not ready however. 然而,在Template.myTemplate.created ,DOM尚未就绪。

If you need to manipulate the DOM, you might want to use Template.myTemplate.rendered instead and use a boolean value to track the state within the Template object like this: 如果需要操作DOM,可能需要使用Template.myTemplate.rendered并使用布尔值来跟踪Template对象中的状态,如下所示:

Template.myTemplate.rendered = function() {
    if(!this._rendered) {
      this._rendered = true;
      console.log('Template onLoad');
    }
}

Following should work. 以下应该工作。
Meteor.defer will get called once template is added to DOM and rendered. 一旦将模板添加到DOM并进行渲染,就会调用Meteor.defer。

<template name="temp">
    //regular stuff
    {{invokeAfterLoad}}
</template>

Template.temp.invokeAfterLoad = function () {
  Meteor.defer(function () {
     $('mydiv').jquerify();
  });
  return "";
};

I'd recommend this rather than the accepted answer, slightly less gross IMHO: 我推荐这个而不是接受的答案,稍微不那么粗略恕我直言:

<template name="temp">
    {{aReactiveHelper}}
</template>

Template.temp.aReactiveHelper = function() {
  var someValue = Session.get('someValue');
  invokeAfterLoad();
  return someValue;
};

var invokeAfterLoad = function () {
  Meteor.defer(function () {
     $('mydiv').doSomething();
  });
};

The assumption is that you want to invoke something after the template loads because it's reacting to a reactive object. 假设您希望在模板加载后调用某些内容,因为它会对反应对象做出反应。

The benefit here is you're not adding animation code to your template. 这里的好处是您不会在模板中添加动画代码。

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

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