gpt4 book ai didi

javascript - polymer 就绪函数仅调用一次

转载 作者:行者123 更新时间:2023-12-02 15:22:01 24 4
gpt4 key购买 nike

我正在测试 Polymer 来开发一个我想在 html 中多次重用的组件(见下图)。我开发了以下元素:

<link rel="import" href="bower_components/polymer/polymer.html">
<script src="js/fabric.js"></script>

<dom-module id="tooth-element">
<template>
<style>
#labelWrapper {
display: flex;
justify-content: center;
align-items: center;
font-size: 35px;
font-weight: bold;
height: 40PX;
}
</style>

<div id="toothWrapper">
<div id="upperRootWrapper">
<canvas id="upperRoot" />
</div>

<div>
<canvas id="tooth" />
</div>

<div id="lowerRootWrapper">
<canvas id="lowerRoot" />
</div>

<div id="labelWrapper">
<p>{{toothLabel}}</p>
</div>
</div>
</template>

<script>
var TOOTH_SIZE = 50;
var TOOTH_GAP = 5;

var UPPERROOTID = 'upperRoot';
var LOWERROOTID = 'lowerRoot';
var TOOTHID = 'tooth';

Polymer({
is: "tooth-element",
properties: {
// declare the owner property
showUpperRoot: {
type: Boolean,
value: true
},
showLowerRoot: {
type: Boolean,
value: true
},
toothLabel: {
type: String,
value: 30
}
},
ready: function() {
drawUpperRoot(this.showUpperRoot);
drawTooth();
drawLowerRoot(this.showLowerRoot);
initLabel(this.toothLabel);
}
});

function initLabel (label) {
document.getElementById("labelWrapper").style.width = new String(3*TOOTH_SIZE)+'px';
}

function drawUpperRoot (isVisible) {
var canvas = new fabric.Canvas(UPPERROOTID);
if (isVisible) {
canvas.setHeight(TOOTH_SIZE+TOOTH_GAP);
canvas.setWidth(TOOTH_SIZE*3+TOOTH_GAP);
canvas.renderAll();
canvas.add(drawFace(1));
} else {
document.getElementById("upperRootWrapper").style.display = "none";
}
}

function drawTooth () {
var canvas = new fabric.Canvas(TOOTHID);
canvas.setHeight(TOOTH_SIZE*3+TOOTH_GAP*2);
canvas.setWidth(TOOTH_SIZE*3+TOOTH_GAP*2);
canvas.renderAll();
canvas.add(drawFace(2));
canvas.add(drawFace(3));
canvas.add(drawFace(4));
canvas.add(drawFace(5));
canvas.add(drawFace(6));
//canvas.add(drawFace(7));
}

function drawLowerRoot (isVisible) {
var canvas = new fabric.Canvas(LOWERROOTID);
if (isVisible) {
canvas.setHeight(TOOTH_SIZE+TOOTH_GAP*2);
canvas.setWidth(TOOTH_SIZE*3+TOOTH_GAP*2);
canvas.renderAll();
canvas.add(drawFace(7));
} else {
document.getElementById("lowerRootWrapper").style.display = "none";;
}
}

function drawFace(face) {
var coordinates;
switch (face) {
case 1:
coordinates = getFace1Coordinates();
break;
case 2:
coordinates = getFace2Coordinates();
break;
case 3:
coordinates = getFace3Coordinates();
break;
case 4:
coordinates = getFace4Coordinates();
break;
case 5:
coordinates = getFace5Coordinates();
break
case 6:
coordinates = getFace6Coordinates();
break;
case 7:
coordinates = getFace7Coordinates();
break;
default:
coorddinates = null;
}

face = new fabric.Polygon(coordinates, {
fill: '#E1E1E1',
selectable: false,
stroke: 'green'
});
face.on('mousedown', function () {
if (face.fill == '#E1E1E1') {
face.fill = '#878787';
} else {
face.fill = '#E1E1E1';
}
});
return face;
}

function getFace1Coordinates() {
return [{x: TOOTH_SIZE, y: 0}, {x: TOOTH_SIZE*2+TOOTH_GAP, y: 0}, {x: TOOTH_SIZE*3+TOOTH_GAP, y: TOOTH_SIZE}, {x: 0, y: TOOTH_SIZE}];
}

function getFace2Coordinates() {
return [{x: 0, y: TOOTH_GAP}, {x: TOOTH_SIZE, y: TOOTH_SIZE+TOOTH_GAP}, {x: TOOTH_SIZE, y: TOOTH_SIZE*2+TOOTH_GAP}, {x: 0, y: TOOTH_SIZE*3}];
}

function getFace3Coordinates() {
return [{x: 0, y: 0}, {x: TOOTH_SIZE, y: TOOTH_SIZE}, {x: TOOTH_SIZE*2, y: TOOTH_SIZE}, {x: TOOTH_SIZE*3, y: 0}];
}

function getFace4Coordinates() {
return [{x: TOOTH_SIZE*3+TOOTH_GAP, y: 0}, {x: TOOTH_SIZE*2+TOOTH_GAP, y: TOOTH_SIZE}, {x: TOOTH_SIZE*2+TOOTH_GAP, y: TOOTH_SIZE*2+TOOTH_GAP}, {x: TOOTH_SIZE*3+TOOTH_GAP, y: TOOTH_SIZE*3}];
}

function getFace5Coordinates() {
return [{x: 0, y: TOOTH_SIZE*3+TOOTH_GAP}, {x: TOOTH_SIZE+TOOTH_GAP, y: TOOTH_SIZE*2+TOOTH_GAP}, {x: TOOTH_SIZE*2, y: TOOTH_SIZE*2+TOOTH_GAP}, {x: TOOTH_SIZE*3+TOOTH_GAP, y: TOOTH_SIZE*3+TOOTH_GAP}];
}

function getFace6Coordinates() {
return [{x: TOOTH_SIZE+TOOTH_GAP, y: TOOTH_SIZE+TOOTH_GAP}, {x: TOOTH_SIZE*2, y: TOOTH_SIZE+TOOTH_GAP}, {x: TOOTH_SIZE*2, y: TOOTH_SIZE*2}, {x: TOOTH_SIZE+TOOTH_GAP, y: TOOTH_SIZE*2}];
}

function getFace7Coordinates() {
return [{x: 0, y: 0}, {x: TOOTH_SIZE, y: TOOTH_SIZE}, {x: TOOTH_SIZE*2, y: TOOTH_SIZE}, {x: TOOTH_SIZE*3, y: 0}];
}
</script>
</dom-module>

我正在尝试将其导入到 index.html 文件中,如下所示:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="tooth-element.html">
</head>

<body>
<div id="line1">
<tooth-element></tooth-element>
<tooth-element></tooth-element>
</div>
</body>
</html>

当我在 div line1 中放置两个齿元素时,就会出现问题。第二个元素永远不会显示,因为永远不会调用它的就绪函数。我不知道到底应该做什么才能解决这个问题。任何帮助将不胜感激

我想在 html 中重复的组件布局如下所示(我得到第一个 dentry 元素的图像,但没有得到第二个 dentry 元素的图像):

Tooth Representation

(解决方案)我没有正确使用 DOM(正如 Neil John Ramal 所解释的)。根据 Glenn 和 Neil 使用 Poly.dom API 的建议,我能够获得我想要的行为

最佳答案

在处理本地 DOM 时,请尽可能避免使用常规 DOM API(即 document.getElementByIddocument.querySelector 等)。请改用Polymer.dom。请注意,当您将 Polymer 设置为使用 Shadow DOM 而不是 Shady DOM 时,您的代码可能会失败,因为常规 DOM 访问器无法穿透 Shadow DOM(封装是其功能之一,Shady DOM 试图近似)。

代码中的问题之一是生成的元素与某些全局对象紧密耦合(例如 drawUpperRoot)。这本质上破坏了您的元素封装,因为此代码也将被该元素的其他实例使用,因此您基本上使它们依赖于全局范围,而不是使用它们自己的范围。

还有这段代码:

function initLabel (label) {
document.getElementById("labelWrapper").style.width = new String(3*TOOTH_SIZE)+'px';
}

意味着当您创建两个tooth-element时,第二个元素基本上会引用第一个元素的labelWrapper而不是它自己的。

https://www.polymer-project.org/1.0/docs/devguide/local-dom.html

关于javascript - polymer 就绪函数仅调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com