简体   繁体   English

在流星模板中包含javascript

[英]Including javascript in a meteor template

I am very new to meteor and am trying to run a page that makes use of flowplayer video player. 我对流星很陌生,正在尝试运行一个使用flowplayer视频播放器的页面。

Normally all I'd need to do to install flowplayer is add one js and css file to the page. 通常,安装flowplayer所需要做的就是在页面中添加一个js和CSS文件。

I've added these files to the /public folder and added links to them in the head. 我已将这些文件添加到/ public文件夹,并在其头部添加了指向它们的链接。 The files appear on the page but the player does not appear as normal - it seems as though in page javascript doesn't run as it would in a standard page. 文件会显示在页面上,但播放器却无法正常显示-好像页面中的javascript不能像在标准页面中那样运行。

One pure js solution is to load the script in the onRendered Funktion: 一种纯粹的js解决方案是将脚本加载到onRendered Funktion中:

Template.yourTemplate.onRendered(function() {
      $(document).ready(function() {
        var script = document.createElement("script");
        script.type="text/javascript";
        script.src = "REMOTE_SCRIPT_URL";
        $("#script_div").append(script);
      });
});

Or you can use for example meteor-external-file-loader for including the external files. 或者,您可以使用例如meteor-external-file-loader来包含外部文件。

You're looking for helpers: 您正在寻找帮助者:

Template.body.helpers({
  tasks: [    
    { text: 'This is task 1' },    
    { text: 'This is task 2' },    
    { text: 'This is task 3' },    
  ],    
});

and in your Template HTML: 并在您的模板HTML中:

  <div class="container">
    <header>    
      <h1>Todo List</h1>    
    </header>     
    <ul>
      {{#each tasks}}    
        {{> task}}    
      {{/each}}    
    </ul>    
  </div>

EDIT : Maybe a better example here: 编辑 :也许是一个更好的例子在这里:

Template.loaderboard.helpers({
    player: function() {
        return "Hello player";
    },
    videoPlayer: function() {
        return $(".video-player").videoPlayer(); // Pseudo-code
    }
});

and in your HTML: 并在您的HTML中:

<template name="leaderboard">
    {{player}}
    {{videoPlayer}}
</template>

如果库仅露出一个全球性的var (而不是全球的窗口),那么你就应该放弃它的[projectRoot]/client/compatibility特殊的文件夹,让流星知道它不应该包裹在一个单独的范围JS文件,但将其作为老式库包含(类似于<script>标记)。

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

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