聚合物 1.0 元素上的动态 类
dynamic classes on polymer 1.0 elements
我创建了这个组件来演示我的问题。正如预期的那样,此组件在 chrome 和 firefox 中工作。但是如果我写 this.$.wrapper.setAttribute('class','blue');
而不是 this.$.wrapper.setAttribute('class','blue style-scope poly-test');
它会停止在 firefox 中工作。
这是在事件处理程序中更改影子 dom 元素上的 classes 的首选方法吗,还是我不小心做了一些正确的事情,这可能
在未来的版本中破解?
此外,为什么我必须为 firefox 手动指定 style-scope
和我的元素名称作为 class?
<link rel="import" href="../js/bower_components/polymer/polymer.html">
<dom-module id="poly-test">
<style>
.blue { border: 10px solid blue; }
.red { border: 10px solid red; }
#wrapper { font-weight: bold; font-size: 42px; }
</style>
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {'blue': { type: 'Boolean', value: false }},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.blue = !this.blue;
if (this.blue) {
this.$.wrapper.setAttribute('class','blue style-scope poly-test');
} else {
this.$.wrapper.setAttribute('class','red style-scope poly-test');
}
this.updateStyles();
}
});
</script>
使用classList
属性管理类:
<link rel="import" href="../js/bower_components/polymer/polymer.html">
<dom-module id="poly-test">
<style>
.blue { border: 10px solid blue; }
.red { border: 10px solid red; }
#wrapper { font-weight: bold; font-size: 42px; }
</style>
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {'blue': { type: 'Boolean', value: false }},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.blue = !this.blue;
this.$.wrapper.classList.toggle('blue', this.blue);
this.$.wrapper.classList.toggle('red', !this.blue)
}
});
</script>
关于 classList
的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
网络组件的理念是让网络尽可能声明式。本着这种精神,实现动态 类 的聚合物方式应该是
声明式方法: (https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#native-binding)
...
<dom-module id="poly-test">
...
<template>
<!-- handle dynamic classes declaratively -->
<div class$="{{computeClass(isBlue)}}">
<content></content>
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {
'isBlue': { type: Boolean, value: false }
},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.isBlue = !this.isBlue;
},
computeClass: function (f) {
return f ? "blue" : "red";
}
});
</script>
style-scope
由框架在将元素升级和将节点标记为 DOM 时使用(我相信是在阴暗的行为下),我认为我们不打算碰它。
如果你真的想强制处理,我会推荐使用Polymer API的toggleClass()
方法。
命令式方法: (http://polymer.github.io/polymer/)
...
<dom-module id="poly-test">
...
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {
'isBlue': { type: Boolean, value: false }
},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.isBlue = !this.isBlue;
this.toggleClass("blue", this.isBlue, this.$.wrapper);
this.toggleClass("red", !this.isBlue, this.$.wrapper);
}
});
</script>
我创建了这个组件来演示我的问题。正如预期的那样,此组件在 chrome 和 firefox 中工作。但是如果我写 this.$.wrapper.setAttribute('class','blue');
而不是 this.$.wrapper.setAttribute('class','blue style-scope poly-test');
它会停止在 firefox 中工作。
这是在事件处理程序中更改影子 dom 元素上的 classes 的首选方法吗,还是我不小心做了一些正确的事情,这可能 在未来的版本中破解?
此外,为什么我必须为 firefox 手动指定 style-scope
和我的元素名称作为 class?
<link rel="import" href="../js/bower_components/polymer/polymer.html">
<dom-module id="poly-test">
<style>
.blue { border: 10px solid blue; }
.red { border: 10px solid red; }
#wrapper { font-weight: bold; font-size: 42px; }
</style>
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {'blue': { type: 'Boolean', value: false }},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.blue = !this.blue;
if (this.blue) {
this.$.wrapper.setAttribute('class','blue style-scope poly-test');
} else {
this.$.wrapper.setAttribute('class','red style-scope poly-test');
}
this.updateStyles();
}
});
</script>
使用classList
属性管理类:
<link rel="import" href="../js/bower_components/polymer/polymer.html">
<dom-module id="poly-test">
<style>
.blue { border: 10px solid blue; }
.red { border: 10px solid red; }
#wrapper { font-weight: bold; font-size: 42px; }
</style>
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {'blue': { type: 'Boolean', value: false }},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.blue = !this.blue;
this.$.wrapper.classList.toggle('blue', this.blue);
this.$.wrapper.classList.toggle('red', !this.blue)
}
});
</script>
关于 classList
的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
网络组件的理念是让网络尽可能声明式。本着这种精神,实现动态 类 的聚合物方式应该是
声明式方法: (https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#native-binding)
...
<dom-module id="poly-test">
...
<template>
<!-- handle dynamic classes declaratively -->
<div class$="{{computeClass(isBlue)}}">
<content></content>
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {
'isBlue': { type: Boolean, value: false }
},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.isBlue = !this.isBlue;
},
computeClass: function (f) {
return f ? "blue" : "red";
}
});
</script>
style-scope
由框架在将元素升级和将节点标记为 DOM 时使用(我相信是在阴暗的行为下),我认为我们不打算碰它。
如果你真的想强制处理,我会推荐使用Polymer API的toggleClass()
方法。
命令式方法: (http://polymer.github.io/polymer/)
...
<dom-module id="poly-test">
...
<template>
<div id="wrapper" class="red"><content></content></div>
</template>
</dom-module>
<script>
Polymer({
is: 'poly-test',
properties: {
'isBlue': { type: Boolean, value: false }
},
listeners: { 'click': 'clickHandler' },
clickHandler: function () {
this.isBlue = !this.isBlue;
this.toggleClass("blue", this.isBlue, this.$.wrapper);
this.toggleClass("red", !this.isBlue, this.$.wrapper);
}
});
</script>