如何在聚合物元件中使用细胞景观?
How to use cytoscape in a polymer element?
这是我的聚合物元素:
<link rel="import" href="../polymer/polymer.html">
<script src="bower_components/cytoscape/dist/cytoscape.js"></script>
<dom-module id="dependency-graph">
<template>
<style>
#surface {
width: 200px;
height: 200px;
}
</style>
<div id="surface"></div>
</template>
<script>
Polymer({
is: 'dependency-graph',
ready: function () {
var surface = this.$.surface;
var cy = cytoscape({
container: surface
});
...
运行 这会在 cytoscape 中引发异常
return ( _p.sizeCache = _p.sizeCache || ( container ? (function(){
var rect = container.getBoundingClientRect();
var style = window.getComputedStyle( container );
var val = function( name ){ return parseFloat( style.getPropertyValue( name ) ); };
return {
width: rect.width - val('padding-left') - val('padding-right') - val('border-left-width') - val('border-right-width'),
height: rect.height - val('padding-top') - val('padding-bottom') - val('border-top-width') - val('border-bottom-width')
};
因为 style.getPropertyValue
returns ""
。如果我使用 document.body
作为容器,它将 return 一个数字和 cytoscape 工作。我做错了什么,我该如何解决?
我找到了一篇关于类似问题的文章,但我没有得到它(或者它不起作用?):webcomponentsjs is not compatible with third-party scripts that use window.getComputedStyle
确保您在 DOM 中已有的元素上调用 Cytoscape 并仅在 DOMContentLoaded
之后初始化。如果 window.getComputedStyle()
不工作,那么至少有一个要求没有得到满足。
我将我的代码移到了 attached: function () {
,现在它可以正常工作了。谢谢@maxkfranz 的想法。
这是我的聚合物元素:
<link rel="import" href="../polymer/polymer.html">
<script src="bower_components/cytoscape/dist/cytoscape.js"></script>
<dom-module id="dependency-graph">
<template>
<style>
#surface {
width: 200px;
height: 200px;
}
</style>
<div id="surface"></div>
</template>
<script>
Polymer({
is: 'dependency-graph',
ready: function () {
var surface = this.$.surface;
var cy = cytoscape({
container: surface
});
...
运行 这会在 cytoscape 中引发异常
return ( _p.sizeCache = _p.sizeCache || ( container ? (function(){
var rect = container.getBoundingClientRect();
var style = window.getComputedStyle( container );
var val = function( name ){ return parseFloat( style.getPropertyValue( name ) ); };
return {
width: rect.width - val('padding-left') - val('padding-right') - val('border-left-width') - val('border-right-width'),
height: rect.height - val('padding-top') - val('padding-bottom') - val('border-top-width') - val('border-bottom-width')
};
因为 style.getPropertyValue
returns ""
。如果我使用 document.body
作为容器,它将 return 一个数字和 cytoscape 工作。我做错了什么,我该如何解决?
我找到了一篇关于类似问题的文章,但我没有得到它(或者它不起作用?):webcomponentsjs is not compatible with third-party scripts that use window.getComputedStyle
确保您在 DOM 中已有的元素上调用 Cytoscape 并仅在 DOMContentLoaded
之后初始化。如果 window.getComputedStyle()
不工作,那么至少有一个要求没有得到满足。
我将我的代码移到了 attached: function () {
,现在它可以正常工作了。谢谢@maxkfranz 的想法。