gpt4 book ai didi

javascript - 如何避免Polymer元素的AttributeChanged事件多次调用

转载 作者:行者123 更新时间:2023-11-28 07:21:51 26 4
gpt4 key购买 nike

实际上,我在我的应用程序中使用 polymer 元素 ABCD 。考虑以下场景。

1) Element A has boolean attribute named 'devEnable'
2) Element A has attributeChanged event named 'devEnableChanged'
3) And my business logic is implemented in 'devEnableChanged' event
4) Other elements --> B extends A , C extends A , D extends A

现在我运行我的应用程序,当属性 devEnable 更改为 true 或 false 时,devEnableChanged 事件将被触发 3 次,因此我的业务逻辑也得到了发射了3次。所以我想避免多次触发 devEnableChanged 事件。

有什么办法可以避免多次调用它吗?

最佳答案

使用基础 polymer 元素(A)作为先前派生 polymer 元素(B、C 或 D)的模板标签内的组合物是一种选择,而不是继承:

index.html:

<!DOCTYPE html>
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
<link rel="import" href="derived-element.html">
</head>
<body>

<derived-element id="derivedElement" value="3546"></derived-element>
<input type="button" id="assignBaseElementButton" value="assign base"/>
<input type="button" id="assignDerivedElementButton" value="assign derived"/>
<input type="text" id="valueField"/>

</body>
<script>

document.getElementById('assignDerivedElementButton').addEventListener('click',function(){

var derivedElement = document.querySelector('#derivedElement');

var textFieldValue = document.getElementById('valueField').value;

// get derived element's attribute value to assign it to a value of valueField textfield
derivedElement.value = textFieldValue;

});

document.getElementById('assignBaseElementButton').addEventListener('click',function(){

var derivedElement = document.querySelector('#derivedElement');

var textFieldValue = document.getElementById('valueField').value;

// get base element's attribute value to assign it to a value of valueField textfield
derivedElement.getBaseElement().value = textFieldValue;

});

</script>
</html>

派生元素.html:

<link rel="import" href="/bower_components/polymer/polymer.html">
<link rel="import" href="/base-element.html">
<polymer-element name="derived-element" attributes="value">
<template>
<!-- embed base element here -->
<base-element id="baseElement"></base-element>
<div>
derived element'value: {{value}}
</div>
</template>
<script>
Polymer('derived-element',{
value: null,
ready:function(){
this.getBaseElement().baseValue = this.value;
},
// watcher for value property
valueChanged: function(oldvalue, newvalue) {

// this method is called once
console.log('my-element', 'oldvalue: ',oldvalue, ' newvalue: ',newvalue);

},
getBaseElement:function(){
return this.$.baseElement;
}
});
</script>
</polymer-element>

基本元素:

<link rel="import" href="/bower_components/polymer/polymer.html">
<polymer-element name="base-element">
<template>
<span>Hello from <b>base-element</b>. This is my Shadow DOM. And my value is {{value}}</span>
</template>
<script>
Polymer('base-element',{
value: null,
// watcher for value property
valueChanged: function(oldvalue, newvalue) {

// this method is called once
console.log('base-element', 'oldvalue: ',oldvalue, ' newvalue: ',newvalue);

}
});
</script>
</polymer-element>

关于javascript - 如何避免Polymer元素的AttributeChanged事件多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214265/

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