I understand the advantages and disadvantages of Aurelia's custom elements vs. <compose>
; Jeremy Danyow's blog post helps. But, I would like to have my cake and eat it too.
I would like to create custom elements that I can also compose dynamically. Since <compose>
requires a different instantiation, to use it would mean that I would need to create two parallel versions of each element -- one for <compose>
and one for static calls. For example, consider the following use case:
<template>
<h1>Welcome to the Data Entry Screen</h1>
<!-- Static controls -->
<my-textbox label="Your name:" value.bind="entry_name"></my-textbox>
<my-datepicker label="Current date:" value.bind="entry_date"></my-datepicker>
<!-- Loop through dynamic form controls -->
<div class="form-group" repeat.for="control of controls" if.bind="control.type !== 'hidden'">
<label class="control-label">${control.label}</label>
<div>
<compose containerless class="form-control"
view-model="resources/elements/${control.type}/${control.type}"
model.bind="{'control': control, 'model': model, 'readonly': readonly}">
</compose>
</div>
</div>
</template>
With the following controls data:
controls = [
{label: 'Entry Date', type: 'my-datepicker', bind: 'acc_entry_date'},
{label: 'Code', type: 'my-textbox', bind: 'acc_entry_code'},
{label: 'Ref', type: 'my-textbox', bind: 'acc_entry_ref'},
{label: 'Description', type: 'my-textarea', rows: '3', bind: 'acc_entry_description'},
{label: 'Status', type: 'my-dropdown', bind: 'acc_entry_status', enum: 'AccountEntryStatus'},
{type: 'hidden', bind: 'acc_entry_period_id'}];
As you can see, I would like to use <my-textbox>
and <my-datepicker>
both statically and dynamically. Custom elements definitely seem like the best approach. However, I don't see how to accomplish this without creating two parallel components -- one designed as a custom element and one designed as a composable view/viewmodel.