在Polymer元素中使用外部脚本

时间:2014-04-07 22:02:48

标签: polymer

"要求"的推荐方式是什么? Polymer元素里面的外部javascript?例如,我正构建一个我要在Slick轮播中显示的视频组组件。

例如,对于自定义元素,我的代码可能如下所示:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymer-jsonp/polymer-jsonp.html">

<polymer-element name="polymer-video-group"  constructor="VideoGroupElement" attributes="">
  <template>
    <style>
      /* styles for the custom element itself - lowest specificity */
      :host {
          display: block;
          position: relative;
      }
      /* 
      style if an ancestor has the different class
      :host(.different) { } 
      */
    </style>

    <!-- Load Data with JSONP Endpoint (in this case Google Spreadsheets)
        Source: https://docs.google.com/spreadsheet/ccc?key=0AqZBbhllhMtHdFhFYnRlZk1zMzVZZU5WRnpLbzFYVFE&usp=sharing
        https://docs.google.com/spreadsheets/d/1CpXbJHeFrzPpg58lWAsT1N3-QExbX9T5OPVeMfyBqYs/pubhtml
    -->
    <polymer-jsonp id="jsonp" url="https://spreadsheets.google.com/feeds/list/1CpXbJHeFrzPpg58lWAsT1N3-QExbX9T5OPVeMfyBqYs/od6/public/values?alt=json-in-script&callback=" response="{{response}}"></polymer-jsonp>

    <template repeat="{{entry in response.feed.entry}}">
      <iframe width="420" height="315" src="//www.youtube.com/embed/{{entry.gsx$id.$t}}" frameborder="0" allowfullscreen></iframe>
    </template>

  </template>
  <script>
    Polymer('polymer-video-group', {

        // element is fully prepared
        ready: function(){
            this.$.jsonp.go();
        },

        // instance of the element is created
        created: function() {
          this.videos = [];
          this.response = {};
        },

        // instance was inserted into the document
        enteredView: function() { },

        // instance was removed from the document
        leftView: function() { },

        // attribute added, removed or updated
        attributeChanged: function(attrName, oldVal, newVal) { },

        // Response from JSONP Data Changed Event Handler
        responseChanged: function() {

            // Get the Entry Point for the JSON Feed
            var entries = this.response.feed.entry;

            // Create an empty variable to store the video group
            var videos = [];

            // Push entries from the JSON feed onto the videos array
            for (var i = 0, entry; entry = entries[i]; ++i) {
                videos.push({
                    name: entry.gsx$name.$t,
                    id: entry.gsx$id.$t
                });
            }

            // Set the video group object's array to this newly supplied video array
            this.videos = videos;
        }
    });
  </script>
</polymer-element>

但是,我不想仅仅在iframe中显示每个视频,而是希望这些视频出现在由Slick提供支持的旋转木马中,所以我想要做一些与以下相关的事情:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymer-jsonp/polymer-jsonp.html">
<script src="../bower_components/slick-carousel/slick/slick.js"></script>

我是否必须创建一个包含光滑功能的自定义元素,或者我可以直接使用上述示例中的资源吗?

更新: 我创建了一个&#34; elements / slick-import.html&#34;文件包含Slick需要的3件事:

<link rel="stylesheet" href="../bower_components/slick-carousel/slick/slick.css"/>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/slick-carousel/slick/slick.js"></script>

在我的elements / video-group.html元素中,我这样引用它:     ...          ...

我注意到该页面包含slick.css文件,但是当页面加载时,Slick所需的其他2个js文件没有附加到DOM。我是否在slick-import.html中正确引用了包含的脚本?

更新2: 这是我真正的问题:我有这个重复的模板打印出我从jsonp响应构建的视频列表:

<div id="carousel">
  <template repeat="{{video in videos}}">
    <div>
      <iframe width="420" height="315" src="//www.youtube.com/embed/{{video.id}}" frameborder="0" allowfullscreen></iframe>
    </div>
  </template>
</div>

但是惹恼的是,Chrome DevTools中生成的DOM显示了这样的标记:

<video-group>
  <div id="carousel" class="slick-initialized slick-slider">
    <template repeat="{{video in videos}}"></template>

    <div>
      <iframe width="420" height="315" src="//www.youtube.com/embed/Fp1wPwszF9M" frameborder="0" allowfullscreen=""></iframe>
    </div>

    <div>
      <iframe width="420" height="315" src="//www.youtube.com/embed/H-l2cq-MXUs" frameborder="0" allowfullscreen=""></iframe>
    </div>

    <div class="slick-list draggable" tabindex="0" style="padding: 0px 50px;">
      <div class="slick-track" style="width: 0px; -webkit-transform: translate3d(-832px, 0px, 0px); opacity: 1;"></div>
    </div>
  </div>

  <!-- Load Data with JSONP Endpoint (in this case Google Spreadsheets)
       Source: https://docs.google.com/spreadsheet/ccc?key=0AqZBbhllhMtHdFhFYnRlZk1zMzVZZU5WRnpLbzFYVFE&usp=sharing
       https://docs.google.com/spreadsheets/d/1CpXbJHeFrzPpg58lWAsT1N3-QExbX9T5OPVeMfyBqYs/pubhtml
  -->
  <polymer-jsonp id="jsonp" url="https://spreadsheets.google.com/feeds/list/1CpXbJHeFrzPpg58lWAsT1N3-QExbX9T5OPVeMfyBqYs/od6/public/values?alt=json-in-script&amp;callback=" response="{{response}}"></polymer-jsonp>
</video-group>

注意div#carousel是如何进行&#34; slick-initialized&#34;和&#34;光滑滑块&#34;。这意味着我的Slick Carousel正好在我的div#carousel DOM元素上运行,但由于嵌套在下面的延迟模板标签,这使得Slick有一些很好的干净简单DIV来处理,就像{{{}中的示例一样3}}演示:

<div class="your-class">
  <div>your content</div>
  <div>your content</div>
  <div>your content</div>
</div>

无论如何我可以通过特定于聚合物的方法解决这个问题,或者以某种方式修改Slick以仅定位div #carousel下面的子div?

1 个答案:

答案 0 :(得分:3)

我不知道光滑的工作方式,但是如果你希望你的元素确保加载第三方库,那么最好为该脚本文件创建一个导入。 Similar question was asked over here