【发布时间】:2014-04-07 22:02:48
【问题描述】:
在 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>
但是,我希望它们出现在由 Slick 提供支持的轮播中,而不是仅在 iframe 中显示每个视频,因此我设想针对以下内容做一些事情:
<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 需要的 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.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&callback=" response="{{response}}"></polymer-jsonp>
</video-group>
请注意 div#carousel 有一个“slick-initialized”和“slick-slider”类。这意味着我的 Slick Carousel 正确地作用于我的 div#carousel DOM 元素,但是由于嵌套在下面的挥之不去的模板标签,这使得 Slick 无法使用一些漂亮干净的简单 DIV 来操作,例如 @987654322 中的示例@演示:
<div class="your-class">
<div>your content</div>
<div>your content</div>
<div>your content</div>
</div>
我是否可以通过特定于 Polymer 的方法或以某种方式修改 Slick 以仅针对 div#carousel 下的子 div 来解决此问题?
【问题讨论】:
标签: polymer