I couldn't seem to get a handle on the data server side so I used a template helper to post the data back in, then copied the area from the piece and ran the items through a re-dot-path/docid routine. Works a treat so far!
template.html
{% import "macros/widgets.html" as widget %}
{%if data.widget%}
{% set section=apos.sectionTemplate.copyme(data.widget) %}
{%endif%}
{{ widget.postWidget(section, 'newnsection') }}
widget.js
const apos = require("apostrophe");
module.exports = {
extend: 'apostrophe-widgets',
label: 'Section Template',
alias: 'sectionTemplate',
defer: true,
addFields: [
{
name: '_nsection',
type: 'joinByOne',
withType: 'templateSections',
label: 'Template',
required: true,
idField: 'id',
filters: {
projection: {
title: 1,
nsection: 1
}
}
}
,
{
name: 'newnsection',
type: 'area',
label: 'Section',
contextual: true,
def: ''
}
],
construct: function (self, options) {
self.addHelpers({
copyme: function (data) {
if (data.newnsection.items.length == 0) {
data.newnsection.items = data._nsection.nsection.items
area = recalcDotPathsAreas(data.newnsection);
function recalcDotPathsAreas(area) {
var docId = area._docId;
var dotPath = area._dotPath;
area.items.forEach(stlevel);
function stlevel(value, index, array) {
var widgetDotPath = dotPath + '.items.' + index;
try {
var $widget = value;
value.__docId = docId;
value.__dotPath = widgetDotPath;
recalcDotPathsAreasWidget(value, docId, widgetDotPath);
} catch (e) {
}
};
return area
};
function recalcDotPathsAreasWidget($widget, docId, dotPath) {
$widget.items.forEach(sclevel);
function sclevel(value, index, array) {
var $area = value;
var areaDocId = value._id;
if ((areaDocId !== docId) && areaDocId && (areaDocId.substring(0, 1) === 'w')) {
value._id = docId;
areaDocId = docId;
}
if (areaDocId === docId) {
var areaDotPath = value._dotPath;
if (areaDotPath) {
var components = areaDotPath.split('.');
var name = components.pop();
value._dotPath = dotPath + '.' + name;
recalculateDotPathsInArea(value);
}
}
};
};
}
return data;
}
});
var superPushAssets = self.pushAssets
self.pushAssets = function () {
superPushAssets()
// self.pushAsset('stylesheet', 'smart-section', {when: 'always'})
self.pushAsset('script', 'always', { when: 'always' })
}
}
}