Combining UI and Bindary
Componyx.UI components and Bindary work independently, but they combine well.
Two common ways to connect a component to your data follow.
Binding through a hidden input
Bind a hidden input to your data model with b-bind, then point a component at it with
ui-hidden-input-id. Any value the component selects is written to the hidden input,
which keeps your data model in sync automatically.
<div b-context="getStarted">
<input type="hidden" id="sizeInput" b-bind="size" />
<cui-combo-box ui-hidden-input-id="sizeInput"
ui-placeholder="- Select -"
ui-item-list='[ { "text": "Small" }, { "text": "Medium" }, { "text": "Large" } ]'>
</cui-combo-box>
<p>
Selected: <span b-if="{{size}}" b-value="size"></span>
<span b-else><i>None selected</i></span>
</p>
</div>
$bindary.appData.getStarted.size = "";
Result
Binding a component property directly
Any HTML attribute can be data-bound with Bindary, including special attributes like
Componyx.UI's ui-* component properties. Wrap a data-key in {{ }}
as the attribute value and the component re-renders whenever that data changes.
ui-observing is what makes that re-render happen. Componyx.UI wasn't built with
Bindary in mind specifically, it's a generic MutationObserver on the component's
own attributes, but that's exactly why the two combine so well: Bindary just updates HTML
attributes like any other script would and Componyx.UI is already listening. Without
ui-observing, only the value present at the moment of the first render is
picked up.
<div b-context="getStarted">
<cui-combo-box id="optionsCombo" ui-placeholder="- Select -"
ui-item-list="{{options}}" ui-observing="true">
</cui-combo-box>
<cui-button ui-text="Add Item" ui-primary="false"
ui-command="$bindary.controller.addOption">
</cui-button>
</div>
$bindary.appData.getStarted.options = [
{ text: "Option 1" },
{ text: "Option 2" },
{ text: "Option 3" }
];
ctrl.addOption = () =>
{
let options = $bindary.appData.getStarted.options;
options.push({ text: `Option ${options.length + 1}` });
$UI.store.optionsCombo.events.onPostRender
.once(() => $UI.store.optionsCombo.showListBox());
};
Result
Starting both in the right order
When a page uses both, one of them has to go first. With an AppSkin, Bindary needs its view element,
so Componyx.UI renders first and then launches Bindary:
(async function ()
{
let uiRendered = false, bindaryReady = false;
const launch = () => { if (uiRendered && bindaryReady) $bindary.launch(); };
$UI.onPostRender.add(() => { uiRendered = true; launch(); }, null, true);
await componyx.bindary_modules.loaded;
$bindary.launchOnLoad = false;
$bindary.addRoute({ path: /^home$/, viewURL: '/views/home.html' });
bindaryReady = true;
launch();
})();
Without an AppSkin, components with {{ }} in their ui-* attributes need
Bindary's values first, so Bindary renders first and then Componyx.UI detects the components:
componyx.UI.autoDetect = false; // in the <head>, right after the Componyx scripts
$bindary.onPostRender.once(() => componyx.UI.detect(null));
See the component reference for every component or the Bindary overview for routing, real-time sync and everything else Bindary can do beyond what's shown here.