简体   繁体   English

无法在Template.onRendered()流星上获取div的高度

[英]Cannot get a div's height on Template.onRendered() Meteor

I'm trying to get the higher height of some the "big-card" in my DOM to put them all at the same height. 我试图在我的DOM中获得一些“大牌”的更高的高度,以将它们全部置于相同的高度。

{{#each skills}}
  <div class="big-card">
    <div class="card-grid add-option-part">
      <div class="card-text">
        <p>{{this}}</p>
      </div>
    </div>
    <div class="option-part">
      <div class="half-option-part white-line-part"><img class="seemore-button" src="/img/expand.png"/></div>
      <div class="half-option-part">{{> StarsRating}}</div>
    </div>
  </div>
  {{/each}}

The function to take get their heights is : 发挥作用的功能是:

function boxContentNormal(){
  var elementHeights = [];

  $('.big-card').map(function() {
    var currentItem = $(this).find('.card-text');
    var currentItemHeight = currentItem.height();
    var currentItemPaddingTop = parseInt(currentItem.css('padding-top').replace("px", ""));
    var currentItemPaddingBottom = parseInt(currentItem.css('padding-bottom').replace("px", ""));
    elementHeights.push(currentItemHeight + currentItemPaddingBottom + currentItemPaddingTop);
  });

  var maxHeight = 0;

  $.each(elementHeights, function(i, element){
    maxHeight = (element > maxHeight) ? element : maxHeight;
  });

  console.log("Max height : "+maxHeight);

}

It's called by that : 它被称为:

Template.MyTemplate.onRendered(function(){
    boxContentNormal();
    $(window).resize(function(){
      boxContentNormal();
    });
  });

This function is used when a new route is called and the template will be displayed at the same time. 调用新路由时将使用此功能,并且模板将同时显示。 It works like that: 它像这样工作:

  1. I click on a link that goes to a new route 我点击了转到新路线的链接
  2. Once arrived to the route, the template will be displayed 到达路线后,将显示模板
  3. When the template is rendered, the function is called for the first time 呈现模板后,将首次调用该函数
  4. After that, if the window resizes the function will be called again 之后,如果窗口调整大小,该函数将再次被调用

The problem is at the third step, when the function is called it doesn't get the height of the cards. 问题出在第三步,当函数被调用时,它没有得到卡片的高度。 Then all the heights are equal to 0. And when I resize the window, it works fine. 然后所有高度都等于0。当我调整窗口大小时,它可以正常工作。

So I think the function is called too early and the "cards" don't exist yet. 因此,我认为该函数的调用还为时过早,而“卡片”尚不存在。 Do you know how I can "wait" for them or another solution ? 您知道我如何“等待”他们或其他解决方案吗?

Thanks :) 谢谢 :)

I suppose your skills helper is returning a cursor from a client side collection synced with the server via the Pub/Sub mechanism. 我想您的skills助手正在通过Pub / Sub机制从与服务器同步的客户端集合中返回一个游标。

You can use the template controller pattern along with template subscriptions to make sure your template is initially rendered after the published data made its way to the client. 您可以将模板控制器模式与模板订阅一起使用,以确保在将发布的数据传递到客户端之后,最初呈现您的模板。

HTML HTML

<template name="skillsController">
  {{#if Template.subscriptionsReady}}
    {{> skillsList items=skills}}
  {{/if}}
</template>

<template name="skillsList">
  {{#each items}}
    {{! skill item}}
  {{/each}}
</template>

JS JS

Template.skillsController.onCreated(function(){
  this.subscribe("skills");
});

Template.skillsController.helpers({
  skills: function(){
    return Skills.find();
  }
});

Template.skillsList.onRendered(function(){
  console.log(this.$(".big-card").length == this.data.items.count());
});

Using this pattern, the skillsList template onRendered life cycle event is executed after the data is already there so the {{#each}} block helper will correctly render its initial list of skill items. 使用此模式, skillsList模板onRendered生命周期事件将在数据已经存在之后执行,因此{{#each}}块帮助程序将正确呈现其技能列表。

If you don't wait for the subscription to be ready, the initial template rendering will run using an {{#each}} fed with an empty cursor. 如果您不等待订阅准备就绪,则初始模板渲染将使用带有空光标的{{#each}}运行。 Once the data arrives, the {{#each}} will rerun and correctly render the items, but the onRendered hook won't. 数据到达后, {{#each}}将重新运行并正确渲染项目,但onRendered挂钩不会。

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

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