我经常遇到向单例添加选项的问题,所以我不经常使用单例,因为我发现您几乎可以在每种情况下使用一个区域。
“据我所知,这需要是一个单例,因为一个区域提供了许多小部件。”
您可以使用只有一种类型的区域,并将小部件数量限制为至少一种。
例如这是我在 gobal 中创建的一个区域:
// lib/modules/apostrophe-global/index.js
{
name: 'footerArea',
label: 'Footer',
help: 'Add a footer here which will be displayed on all pages'
type: 'area',
options: {
limit: 1,
widgets: {
'footer': {
controls: {
movable: false,
cloneable: false,
removable: true,
position: 'top-right'
}
}
}
}
}
不幸的是,这里的限制只影响该区域中的小部件数量,而不影响单个小部件的碎片。
我也想出了joinByArray 解决方案,比如alexbea 说:
{
name: '_images',
type: 'joinByArray',
withType: 'apostrophe-image',
label: 'Images',
help: 'Add images to gallery',
required: true,
limit: 48,
noHeight: true,
filters: {
projection: {
attachment: 1,
description: 1,
title: 1,
titleShow: 1,
titleColor: 1,
titleAlign: 1,
credit: 1,
creditUrl: 1,
clickAction: 1,
gradientColorTop: 1,
gradientColorBottom: 1,
}
}
}
在我的小部件中,我现在可以通过一个简单的循环轻松地显示 _images 的所有内容:
{% for _image in piece._images %}
...
{% endfor %}
在很多情况下,这给了我更多的灵活性,因为我不想按原样展示作品。使用这种方法,我可以在具有不同布局的许多不同小部件中显示一件。使用投影过滤器,我什至可以决定我为这个特定的小部件提取哪些字段。我可以从撇号的可重用性中受益更多,并拥有这样更简洁的代码。