Having trouble with injections in Aurelia and looking for a way to implement Validation, EventAggregator, and Router without using injection.
Check out this example that illustrates the issue I'm facing:
class Profile interacts with the view, creating an object of AddressList that also interacts with the view.
For instance:
@inject(EventAggregator, Validation, Router)
export class Profile{
addressList: Array<AddressList> = [];
eventAgg:any;
_validation:any;
_router:any;
constructor(EventAggregator, Validation, Router )
{
this.eventAgg = EventAggregator;
this._validation = Validation;
this._router = Router;
this.addressList.push(new AddressList());
}
}
export class AddressList{
street1:string = "street1";
street2:string = "street2";
constructor(){
}
Now, the challenge is to implement validations on the properties of AddressList without passing validation in the constructor of AddressList.
Avoiding:
this.addressList.push(new AddressList(Validation));
This approach may pose issues when passing arguments in the AddressList constructor.
Similarly, difficulties can arise when composing one view-model within another where the constructor expects user-defined arguments.
Your insights are appreciated,
Ankur
Question Updates/Changes
I made the suggested changes as per advice from Matthew James Davis. Still puzzled by why AddressList is showing as undefined.
Updated Code Snippet
import { Factory } from 'aurelia-framework';
import { ObserverLocator } from 'aurelia-framework';
import { EventAggregator } from 'aurelia-event-aggregator';
import { Validation, ensure } from 'aurelia-validation';
@inject(EventAggregator, Validation, Factory.of(AddressList))
export class Profile{
addressList: Array<AddressList> = [];
eventAgg:any;
_validation:any;
_router:any;
constructor(EventAggregator, Validation, AddressList)
{
this.eventAgg = EventAggregator;
this._validation = Validation;
this.addressList.push(AddressList(["street1","street2"]));
}
}
@inject(Validation)
export class AddressList{
street1:string = "street1";
street2:string = "street2";
constructor(Validation, args){
this.street1=args[0];
this.street2=args[1];
}
}
Error Message from Console
AddressList
function() {
for (var _len = arguments.length, rest = Array(_len), _key = 0; _key < _len; _key++) {
rest[_key] = arguments[_key];
}
return container.invoke(_this2._…
AddressList ()
https://i.sstatic.net/vHFmw.png
The error seems to be linked to the line in Container.prototype._createInvocationHandler:
if (fn.inject === undefined)
Where fn is undefined.
Hoping this information helps shed light on the issue. Still working on unraveling the root cause.