Edit: This question used to be titled "Getting parent via DI when parent is the same type in Aurelia" but due to how my elements are nested, it makes more sense to just bind the parent to the element, so the title has been changed to reflect this.
If I have a custom element, Thing
which has a child Thing
(which has a another child Thing
, etc), how can I inject the parent instance when the class is the same?
export class Thing {
static inject = [Thing]; // <-- no reference to Thing as we are inside the class
constructor(parentThing) {
this.parent = parentThing;
}
}
As a further complexity, the root Thing
element will have no parent, so the DI needs to allow for optional injection.
I don't think your problem can be solved with DI.
Thing
has to be@transient
if you want to have several instances of it. This means that the container will not hold references to things it creates.This problem doesn't look right or necessary to use DI. If an element need to receive some specific input data from its consumer, @bindable would be my natural first thinking. So how about creating a
@bindable parentThing
inThing
?In other hand, if what you want is to access parent binding context, consider
bind()
component life cycle.Here's how to do that: https://gist.run?id=b075366d29f2400d3cc931f6fc26db24
app.html
app.js
optional-parent.js
thing.html
thing.js