【问题标题】:How to include raw JavaScript code in a Meteor app如何在 Meteor 应用程序中包含原始 JavaScript 代码
【发布时间】:2017-07-04 21:24:53
【问题描述】:

我有一个在 html 页面上创建动画的 javascript。在 Meteor 项目之外,动画效果完美,因为它只是一个包含在 html 文件中的 .js 文件。我怎样才能让它在 Meteor 上工作呢? javascript 文件似乎根本没有运行(它什么也没做)。 Meteor 可以包含原始 javscript 文件吗?如果是这样怎么办?该文件是纯 JavaScript,没有帮助程序或任何定义 Meteor 结构的东西。

missionPage.js

var TxtRotate = function(el, toRotate, period) {
    this.toRotate = toRotate;
    this.el = el;
    this.loopNum = 0;
    this.period = parseInt(period, 10) || 2000;
    this.txt = '';
    this.tick();
    this.isDeleting = false;
 };

 TxtRotate.prototype.tick = function() {
    var i = this.loopNum % this.toRotate.length;
    var fullTxt = this.toRotate[i];
    if (this.isDeleting) {
      this.txt = fullTxt.substring(0, this.txt.length - 1);
    } else {
      this.txt = fullTxt.substring(0, this.txt.length + 1);
    }
    this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';
    var that = this;
    var delta = 300 - Math.random() * 100;
    if (this.isDeleting) { delta /= 2; }
    if (!this.isDeleting && this.txt === fullTxt) {
      delta = this.period;
      this.isDeleting = true;
    } else if (this.isDeleting && this.txt === '') {
      this.isDeleting = false;
      this.loopNum++;
      delta = 500;
    }

  setTimeout(function() {
  that.tick();
  }, delta);
  };

  window.onload= function() {
  console.log('I m here');
  var elements = document.getElementsByClassName('txt-rotate');
  console.log(elements);
      for (var i=0; i<elements.length; i++) {
      var toRotate = elements[i].getAttribute('data-rotate');
      var period = elements[i].getAttribute('data-period');
      if (toRotate) {
      new TxtRotate(elements[i], JSON.parse(toRotate), period);
    }
    }
// INJECT CSS
    var css = document.createElement("style");
    css.type = "text/css";
    css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #666 
  }";
    document.body.appendChild(css);
  };

missionPage.html

<template name="missionPage">
  <div class="ui center aligned container">
    <h3>Our story</h3>
    <h1>This website is
      <span
     class="txt-rotate"
     data-period="2000"
     data-rotate='[ "not just the website", "simple.", "pure JS.", 
 "pretty.", "fun!" ]'></span>
    </h1>
  </div>
</template>

【问题讨论】:

    标签: javascript jquery html meteor meteor-blaze


    【解决方案1】:

    你读过Guide to Meteor, specifically the section on app structure吗?如果没有,我会从那里开始。

    回答您的问题:

    Meteor 可以包含原始 JavaScript 文件吗?

    是的。

    如果是这样怎么办?

    任意数量的方式。可以直接导入:

    import './txtRotate.js';
    

    或者你可以把它放在client/compatibility目录下,它会在其他JS文件之前被加载和执行。

    最后,您的missionPage.js 文件中应该只有MissionPage Template 相关代码,而不是TxtRotate 代码。将TxtRotate移动到自己的文件中(不包括window.onload部分),导入到missionPage.js中,并在模板渲染时初始化:

    import { Template } from 'meteor/templating';
    import './txtRotate.js';
    
    Template.missionPage.onRendered(function() {
      this.$('.txt-rotate').each(function(i, element) {
        var toRotate = $(element).data('rotate');
        var period = $(element).data('period');
    
        if (toRotate) {
          new TxtRotate(this.get(0), JSON.parse(toRotate), period);
        }
      });
    });
    

    您的另一个选择是将您的 TxtRotate 函数更改为 reusable Blaze Component 而不是独立函数。比如:

    <template name="missionPage">
      <div class="ui center aligned container">
        <h3>Our story</h3>
        <h1>This website is {{> TxtRotate period="2000" rotate=words}}</h1>
      </div>
    </template>
    

    words 将在模板助手中定义。

    【讨论】:

    • 非常感谢! @chazsolo
    • 我有一个问题。 // 在这里初始化 txtRotate 是什么意思?我到底要在那里写什么? @chazsolo
    • 这就是您在最初编写的window.onload 函数中所做的事情,或者类似的事情。
    • @Roberto 我为你添加了一些代码——检查一下。请注意,未经测试。
    猜你喜欢
    • 2018-02-03
    • 2016-07-31
    • 2014-04-05
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多