I am facing one issue related to the injection(s) in Aurelia. I was wondering how to implement Validation, EventAggregator and Router without injection.
Below you can find an example which may give you a clear picture about the implementation and where I am stuck.
class Profile interacts with the view, and object of AddressList is created in the profile class and this object(AddressList) interacts with the view.
For example:
@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 I want to implement validations on the properties of AddressList without passing Validation in the construtor of AddressList
I don't want this
this.addressList.push(new AddressList(Valdiation));
Because this will create issues when I want to pass arguments in the constructor of the AddressList.
I think this issue will also occur when we will try to compose one view-model in the other view-model and constructor expects some user defined arguments.
Thanks in advance,
Ankur
Updates/Changes in the questions
I done the changes as suggested by Matthew James Davis. But I am unable to understand why AddressList is coming as undefined.
Updated Code
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 in the 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 ()
Error is due to this line in Container.prototype._createInvocationHandler:
if (fn.inject === undefined)
fn in undefined.
I think it might help you, and I am still trying to figure out what can be the issue.