Use Validation on class object without injection i

2019-07-11 23:36发布

问题:

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.

回答1:

As Patrick noted, you can achieve this using Aurelia's Factory resolver:

import { Factory } from 'aurelia-framework';

@inject(Factory.of(AddressList))
export class Profile {

    addressList: Array<AddressList> = [];

    constructor(AddressList) {
        this.addressList.push(
            AddressList(['123 Elm St.', 'Apt B.'])
        );
    }
}

@inject(Validation)
export class AddressList {

    street1;
    street2;

    constructor(Validation, addressList: string[]) {
        this._validation = Validation;
        this.street1 = addressList[0];
        this.street2 = addressList[1];
    }
}


回答2:

I'm not sure if this is the problem, but as I'm using typescript, I know that you also need to import inject from aurelia-framework.

Another possibility would be to inject it like this:

private static inject = [EventAggregator, Validation, Factory.of(AddressList)]

But if you're not using typescript, I'm not sure if this is necessary.