【问题标题】:Does jquery function within a Template.contentBlock in MeteorJS?jquery 是否在 MeteorJS 的 Template.contentBlock 中起作用?
【发布时间】:2016-10-04 13:54:56
【问题描述】:

我在布局中使用 Template.contentBlock 并尝试调用 Fullcalendar onRendered。

布局

<template name="layout">
{{#if appReady}}

<div class="container-fluid">
    {{>header}}
    {{>errors}}
    <div class="row">
        <div class="col-md-2">
            {{> sidebar}}
        </div>
        <div class="col-md-10">
            {{> Template.contentBlock}}
        </div>
    </div>
</div>

{{else}}
{{>loading}}
{{/if}}

模板

Template.userAppointments.onRendered(function() {
  console.log('userAppointments rendered')
  $('#calendar').fullCalendar();
  $('.row').css("background-color","blue");
});

Console.log 正在显示,但 fullCalendar 引用或更改行均未显示。这让我觉得 jquery 在 Template.contentBlock 中不知何故。

提前感谢您的启发。

【问题讨论】:

  • console.log($)打印的是什么?
  • @4castle ( selector, context ) { // 70 // jQuery 对象实际上只是 init 构造函数 'enhanced' ...
  • 要引用模板的 jQuery 对象,您需要显式使用this.$。所以这部分代码应该可以工作。在你的onRendered 函数中放置一个断点并尝试查看$('.row')$('#calendar') 返回什么。很确定他们不在那里(也许是因为appReady 是假的?)。

标签: jquery meteor fullcalendar


【解决方案1】:

我发现了这个问题。我在 userAppointments 模板中使用了一个模式来检查订阅是否准备就绪。

助手

Template.userAppointments.helpers({
  ready: function () {
    return Template.instance().subscriptionsReady();
  }
});

模板

<template name="userAppointments">
  {{#if ready}}
    ...
  {{/if}}
</template>

由于某种原因,此模式阻止 jquery 在 onRendered 函数中运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多