用于功能模块化的 Polymer script only 组件
Polymer script only component for function modularization
我可以在 Polymer
中使用仅脚本组件来保存我在应用程序中使用的所有辅助函数吗?我不确定具有可跨组件导入的可重用 functions
、constants
的推荐方法是什么?
<dom-module id="helper-functions">
<script>
(function() {
var helper1 = function() { ... };
var helper2 = function() { ... };
Polymer({
is : 'helper-functions'
});
})();
</script>
</dom-module>
你 "could" 这样做,但这取决于那些辅助函数正在做什么以及它们是否需要任何 "Polymer" 功能。
包装这类东西的一种方法是 "behavior",这似乎是 Polymer Elements 本身做事的方式。将你的助手分成功能区域,使每个区域成为一个单独的行为,然后将行为包含在需要它的那些元素中。这是一个展示它是如何完成的示例(我将我的所有行为都包含在 PAS 命名空间中。
<link rel="import" href="../polymer/polymer.html">
<script>
window.PAS = window.PAS || {};
(function() {
'use strict';
var dialogs = [];
PAS.DialogBehaviour = {
attached: function() {
dialogs.push(this); //Capture all dialogs as they start up
},
_requestClose: function() {
this.async(function() { //Wait a second to allow inflight ajax to have a chance to finish
dialogs.forEach(function(dialog) {
if (dialog !== this) {
dialog._forceClose();
}
},this);
},1000);
},
_forceClose: function() {
//abstract
}
};
})();
</script>
然后我将它包含在我的元素中,例如...
Polymer({
is: 'pas-standin',
behaviors: [AKC.Route,PAS.DialogBehaviour,Polymer.NeonAnimatableBehavior],
listeners: {
'pas-error': '_closeDialog'
},
但是对于纯 javascript 函数,我在我的 app.js 文件中添加了辅助函数。我目前没有那么多,我怀疑如果我这样做了,那就表明我没有设计正确的元素。
我可以在 Polymer
中使用仅脚本组件来保存我在应用程序中使用的所有辅助函数吗?我不确定具有可跨组件导入的可重用 functions
、constants
的推荐方法是什么?
<dom-module id="helper-functions">
<script>
(function() {
var helper1 = function() { ... };
var helper2 = function() { ... };
Polymer({
is : 'helper-functions'
});
})();
</script>
</dom-module>
你 "could" 这样做,但这取决于那些辅助函数正在做什么以及它们是否需要任何 "Polymer" 功能。
包装这类东西的一种方法是 "behavior",这似乎是 Polymer Elements 本身做事的方式。将你的助手分成功能区域,使每个区域成为一个单独的行为,然后将行为包含在需要它的那些元素中。这是一个展示它是如何完成的示例(我将我的所有行为都包含在 PAS 命名空间中。
<link rel="import" href="../polymer/polymer.html">
<script>
window.PAS = window.PAS || {};
(function() {
'use strict';
var dialogs = [];
PAS.DialogBehaviour = {
attached: function() {
dialogs.push(this); //Capture all dialogs as they start up
},
_requestClose: function() {
this.async(function() { //Wait a second to allow inflight ajax to have a chance to finish
dialogs.forEach(function(dialog) {
if (dialog !== this) {
dialog._forceClose();
}
},this);
},1000);
},
_forceClose: function() {
//abstract
}
};
})();
</script>
然后我将它包含在我的元素中,例如...
Polymer({
is: 'pas-standin',
behaviors: [AKC.Route,PAS.DialogBehaviour,Polymer.NeonAnimatableBehavior],
listeners: {
'pas-error': '_closeDialog'
},
但是对于纯 javascript 函数,我在我的 app.js 文件中添加了辅助函数。我目前没有那么多,我怀疑如果我这样做了,那就表明我没有设计正确的元素。