Ari*_*iod 3 javascript web-component polymer
在Polymer元素中存储私有状态属性的推荐做法是什么?例如,仅对内部渲染有意义的属性(例如,一些布尔标志指示元素的哪些部分被渲染,或者临时数组是从dom-repeat可以迭代的对象构建的).它们并不意味着通过元素的API公开,仅供内部使用.
我到目前为止所做的是声明可以通过properties对象中的元素API使用的属性,而"私有"属性已经设置在ready和其他函数(例如this._tempArray = [])中,而没有明确地声明它们properties.我不知道这是不是一个好主意?
<dom-module id="test">
<template>
<style>
</style>
<template is="dom-if" if="[[_isDataReady]]">
</template>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'test',
properties: {
dataUrl: {
type: String
}
},
ready: function() {
this._isDataReady = false;
this._tempArray = [];
// Get data asynchronously from dataUrl
// ...
}
});
})();
</script>
</dom-module>
Run Code Online (Sandbox Code Playgroud)
执行此操作的最佳方法是将属性声明为普通属性,但在名称前面加上下划线(_)前缀,并将该属性设置为只读,以便外部使用者不能覆盖该变量.
例如:
properties: {
_isDataReady: {
type: Boolean,
value: false,
readOnly: true
}
}
ready: function () {
...
this.async(function () {
//Some async logic
...
this._set_isDataReady(true); //You will need to use this special generated setter to set the value
);
}
Run Code Online (Sandbox Code Playgroud)
此方法向消费者传达他们不应该使用此属性,因为它是内部属性,并且只读属性可以防止在正常工作流程之外错误地设置属性.
| 归档时间: |
|
| 查看次数: |
1469 次 |
| 最近记录: |