Trying out Aurelia features, and I wanted to create a simple custom attribute, that injects content into element on which attribute is defined, based on the template associated with attribute. So far I had no luck by just creating view and view model for custom attribute, and annotating element with attribute. How does one render template associated with custom attribute. Any links or info would be appreciated.
Following Charleh's link I tried to implement it, and although view renders, it does not bind my items. Here is the code, maybe somebody can spot what is wrong
ctest.ts
import { inject, dynamicOptions, Container, customAttribute, bindable} from "aurelia-framework";
import {ViewEngine} from 'aurelia-templating';
@dynamicOptions
@customAttribute("my-test")
@inject(Element, Container, ViewEngine)
export class MyTestCustomAttribute { // extends Paging {
constructor(private element: any,
private container: Container,
private viewEngine: ViewEngine) {
this.element = element;
}
@bindable items = new Array<number>();
@bindable totalItems: any;
attached() {
for (let i = 0; i < this.totalItems; i++) {
this.items.push(i);
}
this.viewEngine.loadViewFactory('components/ctest/ctest.html').then(factory => {
const childContainer = this.container.createChild();
const view = factory.create(childContainer);
view.bind(this);
});
}
propertyChanged(name, newValue, oldValue) {
switch (name) {
case 'totalItems':
alert("totalItems changed");
break;
case 'items':
alert("items changed");
break;
default:
break;
}
}
}
ctest.html
<template>
hello from my-test
<ul>
<li repeat.for="item of items">
${item}
</li>
</ul>
</template>
and usage
tried also
<div my-test="totalItems.bind:5"></div>
No matter what, this.totalItems
is always undefined.
Update
correct syntax for binding pascal case attribute names per convention is to use "-" so this is correct
<div my-test="total-items:5"></div>
<div my-test="total-items.bind:5"></div>
. – jmvtrinidad