Polymer 在聚合元素内使用外部脚本

Polymer 在聚合元素内使用外部脚本,polymer,Polymer,在聚合元素内部“需要”外部javascript的推荐方法是什么?例如,我正在构建一个视频组组件,我想在旋转木马中显示它 例如,对于自定义元素,我的代码可能如下所示: <link rel="import" href="../bower_components/polymer/polymer.html"> <link rel="import" href="../bower_components/polymer-jsonp/polymer-jsonp.html"> <po

在聚合元素内部“需要”外部javascript的推荐方法是什么?例如,我正在构建一个视频组组件,我想在旋转木马中显示它

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

<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>

/*自定义元素本身的样式-最低特殊性*/
:主持人{
显示:块;
位置:相对位置;
}
/* 
如果祖先具有不同的类,则为样式
:主机(.different){}
*/
聚合物(“聚合物-视频-基团”{
//元素已完全准备好
就绪:函数(){
这是$.jsonp.go();
},
//创建元素的实例
已创建:函数(){
这个是.videos=[];
this.response={};
},
//实例已插入到文档中
enteredView:函数(){},
//实例已从文档中删除
leftView:函数(){},
//添加、删除或更新的属性
attributeChanged:函数(attrName、oldVal、newVal){},
//来自JSONP数据更改事件处理程序的响应
响应更改:函数(){
//获取JSON提要的入口点
var entries=this.response.feed.entry;
//创建一个空变量来存储视频组
var视频=[];
//将JSON提要中的条目推送到视频数组中
对于(变量i=0,条目;条目=条目[i];+i){
视频。推送({
名称:entry.gsx$name.$t,
id:entry.gsx$id.$t
});
}
//将视频组对象的数组设置为此新提供的视频数组
这个。视频=视频;
}
});
但是,我不想只在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>

我是否必须创建一个自定义元素来包装slick的功能,还是可以像上面的示例那样直接使用这些资产

更新: 我创建了一个“elements/slick import.html”文件,其中包括slick需要的三件事:

<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>

在my 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如何具有“slick initialized”和“slick slider”类。这意味着我的Slick Carousel正确地作用于我的div#Carousel DOM元素,但由于下面嵌套了挥之不去的模板标记,这使Slick无法使用一些干净的简单div,如演示中的示例:

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

你的内容
你的内容
你的内容

我有没有办法解决这个问题,通过特定于聚合物的方法,或者以某种方式修改Slick以只针对div#carousel?下面的子div

我不知道Slick如何工作,但是如果您希望元素确保加载第三方库,那么最好为该脚本文件创建导入

谢谢罗布的帮助。从技术上讲,我成功导入了流畅的javascript和CSS文件。我按照你的建议做了,创建了一个import.html,其中包含了Slick需要使用的资产,它允许我从组件内部使用它,这很好。唯一还没有解决的问题是,当Polymer解析自定义元素时,会出现一些问题,比如如何巧妙地处理生成的DOM,我在最初的问题中对其进行了更新。我可能也会为这个问题创建一个单独的SO问题…更新:我让这个工作。可能重复的