if I have a view-model that I intend to compose, how can I @inject
an instance of that view-model into another control to compose without breaking the @inject(Element)
on the injected view-model.
Here is what I would like to do:
// input control
import { inject } from 'aurelia-framework';
@inject(Element)
export class input {
constructor(element, options) {
//ref to element should be the input control
}
//other stuff
}
//Form control that might need an input
import { inject, Factory, Container } from 'aurelia-framework';
import input from 'control/input';
@inject(Factory.of(input))
export class form {
constructor(getInput) {
this.controls = [];
this.getInput = getInput;
//when input is needed in this form
}
build(controls) {
for (let control in controls) {
this.controls.push(this.getInput(control));
}
}
}
<!-- Form View -->
<template>
<form >
<template repeat.for="control of controls">
<compose containerless view-model.bind="control"></compose>
</template>
</form>
</template>
Now this is a very trimmed down example and it unfortunately does not work the way I would expect. When the input is pre-built using the Factory the Element that is injected into the constructor is not the input document fragment you would get if you composed the control normally and passed the options into the activate method. Now with this example the obvious solution would be to do this:
// input control
import { inject } from 'aurelia-framework';
@inject(Element)
export class input {
constructor(element) {
//ref to element should be the input control
}
activate(options){
//other stuff
}
}
//Form control that might need an input
import { inject, Factory, Container } from 'aurelia-framework';
import input from 'control/input';
export class form {
constructor(getInput) {
this.controls = [];
this.inputVM = input;
}
build(controls) {
this.controls = controls;
}
}
<!-- Form View -->
<template>
<form >
<template repeat.for="control of controls">
<compose containerless view-model.bind="$parent.inputVM" model.bind="control"></compose>
</template>
</form>
</template>
But with this solution we no longer have a reference to the array of control instances which is needed to handle other functionality. I am looking for a way to “pre-compose” a view-model to get an immediate reference to its instance.