The simplest form
A component does not have to live anywhere special. Mark markup already on the page as
includable, include it and put the component attribute on the
including element. Bindary calls the initiator and the included markup binds against the instance it
returns.
Multiple instances of the same includable template need a repeat
around them, one instance per item, rather than several separate b-include elements written out
by hand.
The including element does not have a context of its own yet when the initiator runs, it is about to receive
the component's context instead. Read the repeat item from templateItem.parent.context, one
level up, not from templateItem.context. Anything the component itself needs to observe for
changes has to be an object, not a bare value, wrap it, {{ count: 0 }} rather than plain count: 0,
since b-observe watches an object's properties, not a primitive on its own.
<div id="counterTemplate" b-includable>
<b b-value="label"></b>: <span b-value="counter.count"></span>
<a href="#" b-on-click="$component.increment">more</a>
</div>
<div b-repeat="counters">
<div b-include="#counterTemplate" b-component="counter" b-observe="counter"></div>
</div>
$bindary.appData.demo.component.counters = [
{ label: 'Apples' },
{ label: 'Oranges' }
];
$bindary.addComponent('counter', function (templateItem)
{
return {
label: templateItem.parent.context.label, // the repeat item, one level up
counter: { count: 0 }, // observed property must live on an object
increment: function (item, e) { e.preventDefault(); ++this.counter.count; }
};
});
Result
Two instances, one template, no file and no class. Each holds its own counter.count because each
got its own object from the initiator. That is the whole mechanism; everything else is about where the
component comes from.