API Status: Development
(require("bajaux/spandrel"))(arg, paramsopt) → {function}
- Description:
The purpose of
spandrelis to provide a reasonably pure-functional,
diffable method of defining a nested structure of bajaux Widgets and
supporting HTML. Rather than require Widget implementors to manually code
calls toinitialize()orbuildFor(),spandrelallows you to provide
your desired structure of HTML elements and their associated Widget
instances, and handle the work of updating the document as that structure
may change over time.See Building Composite Widgets With spandrel for in-depth information.
- Source:
- Since:
- Niagara 4.10
Examples
Generate a static widget
const StaticWidget = spandrel([
'<label>Name: </label>',
'<input type="text" value="{{ props.name }}">',
{
dom: '<span></span>',
value: false,
properties: 'inherit'
}
]);
return fe.buildFor({
dom: $('#myStaticWidget'),
type: StaticWidget,
properties: { name: 'Logan', trueText: 'Good', falseText: 'Not So Good' }
});
Generate a dynamic widget with a field editor for each slot
const DynamicWidget = spandrel(comp => comp.getSlots().toArray().map(slot => ({
dom: '<div class="componentSlot"/>',
kids: [
`<label>${ slot.getName() }: </label>`,
{ dom: '<span/>', complex: comp, slot: slot }
]
})));
return fe.buildFor({
dom: $('#myDynamicWidget'),
type: DynamicWidget,
value: myComponent
});
Subclass an existing dynamic spandrel widget, making changes before rendering.
// our superclass will render a <label> element, with a background
// determined by a widget property.
const LabelWidget = spandrel((value, { properties }) => {
const label = document.createElement('label');
label.innerText = value;
label.style.background = properties.background || '';
return label;
});
const RedLabelWidget = spandrel((value, { renderSuper }) => {
// renderSuper will call back to the superclass, allowing your subclass
// to edit the data before spandrel renders it to the page.
//
// you can optionally pass a function to renderSuper that will tweak the
// widget state before the superclass renders its data. if no tweaking is
// desired, just renderSuper() is fine.
//
return renderSuper((state) => {
state.properties.background = 'lightpink';
// remember to return the new state.
return state;
})
.then((label) => {
// renderSuper will resolve the data exactly as rendered by the
// superclass.
label.style.color = 'red';
return label;
});
}, { extends: LabelWidget });
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arg |
module:bajaux/spandrel~SpandrelData | function | |||||||||||||||||||
params |
object |
<optional> |
{}
|
params Properties
|
Returns:
a Widget constructor
- Type
- function
- Description:
API Status: Development
- Source:
Members
(static) jsx
- Description:
Use
spandrel.jsxas your JSX pragma to convert your JSX into spandrel
config.
- Source:
- See:
Use spandrel.jsx as your JSX pragma to convert your JSX into spandrel
config.