Skip to content

[Proposal] Enhance Html only element #301

@bigopon

Description

@bigopon

I'm submitting a feature request

  • Library Version:
    1.4.0

Current behavior:
Currently, one can only define html only element in following way:

<!-- Globally available -->
<!-- my-input.html -->
<template bindable='name, value'>
  <label>
    ${name}
    <input value.bind='value' />
  </label>
</template>

<!-- app.html -->
<template>
  <my-input value.two-way='appInputValue' name.bind='appInputLabel' />
</template>

Binding mode has to be specified whenever my-input is consumed.

Desired behavior:

Ability to define binding mode in html only element definition, in following syntax:

bindable = [prop] & [bindingMode = oneWay]

Example:

<!-- my-input.html -->
<template bindable='value & twoWay, name'>
  <label>
    ${name}
    <input value.bind='value' />
  </label>
</template>

<!-- app.html -->
<template>
  <my-input value.bind='appInputValue' name.bind='appInputLabel' />
</template>

Breaking changes: none. As user needs to define bindingMode explicitly otherwise resolves to oneWay, which is the current behavior.

Changes require

In dynamic-element.js.

/*eslint padded-blocks:0*/
import {useView, customElement, bindable} from 'aurelia-templating';
import {bindingMode} from 'aurelia-binding'; // <------- new

export function _createDynamicElement(name: string, viewUrl: string, bindableNames: string[]): Function {
  @customElement(name)
  @useView(viewUrl)
  class DynamicElement {
    bind(bindingContext) {
      this.$parent = bindingContext;
    }
  }
  for (let i = 0, ii = bindableNames.length; i < ii; ++i) {
    let parts = bindableName[i].split('&');
    let config;
    if (parts.length === 2) {
      config = {
        name: parts[0].trim(),
        defaultBindingModel: parts[1].trim()
      }
    } else {
      config = parts[0].trim();
    }
    bindable(config)(DynamicElement);
  }
  return DynamicElement;
}

Further enhancements

Syntax could be extended to also support notifying changes via event. This is to support common pattern prop-down event up

bindable = [prop] ^ [event-to-dispatch-to-notify-change] & [bindingMode = oneWay]

example:

<!-- my-input.html -->
<template bindable='value ^ valueChanged, name'>
  <label>
    ${name}
    <input value.bind='value' />
  </label>
</template>

<!-- app.html -->
<template>
  <my-input value.bind='appInputValue' name.bind='appInputLabel' valueChanged.trigger='inputChanged($event.detail)' />
</template>

Draft implementation: Gist;

  • What is the motivation / use case for changing the behavior?

This enhancement, followed by let element for declaration, will make development in Aurelia truly pleasure experience.

cc @EisenbergEffect @jdanyow

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions