gpt4 book ai didi

Javascript 绑定(bind)函数到 Shadow DOM 和自定义元素

转载 作者:行者123 更新时间:2023-12-03 11:08:06 25 4
gpt4 key购买 nike

我正在使用Polymer在一个项目上。我有一个通用的自定义元素,它封装了我的整个项目,我们称之为 <my-app></my-app> 。我希望每当按钮( <paper-button><paper-item><my-custom-clickable-item> ,...)时播放声音。声音的播放没有问题,但我不太知道如何有效地将函数绑定(bind)到我想要的所有元素。

我知道有办法 fire custom events ,但是如果<my-custom-clickable-item>将在另一个元素 <another-custom-element> 内,我需要在那里捕获它并将其传递给 <my-app> ,这也不是一个很好的解决方案。

除了仅将声音绑定(bind)到按钮之外,理想的情况是根据该项目是否具有 disabled属性将播放另一个声音。

这里是一些示例代码,展示了我的应用程序的外观。

<polymer-element name="my-app">
<template>
<audio id="mySound" style="display: none;">
<source src="assets/mySound.wav" type="audio/wav">
</audio>
<paper-button>I make sound!</paper-button>
<my-custom-clickable-item>I make sound as well!</my-custom-clickable-item>

<paper-button disabled>I should make another sound though...</paper-button>

<just-another-element></just-another-element>
</template>
<script>
Polymer('my-app', {
makeNoise: function(){
this.$.mySound.play();
}
});
</script>
</polymer-element>

<polymer-element name="just-another-element">
<template>
<p>I am another custom element, with other buttons!</p>
<paper-button>Sound sound sound</paper-button>
</template>
<script>
Polymer();
</script>
</polymer-element>

最佳答案

即使在其他元素内部,事件也可以毫无问题地冒泡。您可以使用 fire 方法添加有关发件人项目的详细信息,例如它是否已禁用。然后在主事件句柄中,您可以根据该详细信息播放不同的声音。

这里有一个简单的例子:

<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>seed-element Demo</title>
<script src="https://www.polymer-project.org/components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="https://www.polymer-project.org/components/polymer/polymer.html">
<link rel="import" href="https://www.polymer-project.org/components/core-elements/core-elements.html">
<link rel="import" href="https://www.polymer-project.org/components/paper-elements/paper-elements.html">

</head>

<body>

<my-app></my-app>


<polymer-element name="my-app">
<template>
<style>
paper-button[disabled] {
pointer-events: all !important;
}
</style>
<audio id="soundWhenEnabled" style="display: none;">
<source src="assets/mySoundEnabled.wav" type="audio/wav">
</audio>
<audio id="soundWhenDisabled" style="display: none;">
<source src="assets/mySoundDisabled.wav" type="audio/wav">
</audio>
<paper-button id="b1" raised on-click="{{onClick}}">I make sound!</paper-button>
<my-custom-clickable-item id="b2" on-click="{{onClick}}">I make sound as well!</my-custom-clickable-item>

<paper-button id="b3" disabled raised on-click="{{onClick}}">I should make another sound though...</paper-button>

<just-another-element></just-another-element>
</template>
<script>
Polymer('my-app', {
eventDelegates: {
//click: 'onClick',
sound: 'makeNoise'
},
onClick: function(event, detail, sender) {
console.log("Click "+sender.hasAttribute('disabled'))
this.fire('sound', {id:sender.id, disabled: sender.hasAttribute('disabled')});
},
makeNoise: function(event, detail, sender){
console.log(detail)
if (detail.disabled) {
//this.$.soundWhenDisabled.play();
alert("I make noise disabled");
} else {
//this.$.soundWhenEnabled.play();
alert("I make noise enabled");
}
}
});
</script>
</polymer-element>

<polymer-element name="just-another-element">
<template>
<p>I am another custom element, with other buttons!</p>
<paper-button id="b4" raised on-click="{{onClick}}">Sound sound sound</paper-button>
</template>
<script>
Polymer('just-another-element', {
onClick: function(event, detail, sender) {
console.log("Click "+sender.hasAttribute('disabled'))
this.fire('sound', {id:sender.id, disabled: sender.hasAttribute('disabled')});
}
});
</script>
</polymer-element>

</body>
</html>

骗子在这里:http://plnkr.co/edit/SOwYbU3Fvlhp0MgvM60H?p=preview

我希望它有帮助:)像往常一样,不要犹豫询问精度

关于Javascript 绑定(bind)函数到 Shadow DOM 和自定义元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747975/

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