性能提升(Angular)

1.Use map to cache validator (validator should be the same of each formgroup in formarray)

  formControlInitByCaps(formControl: AbstractControl, caps: MetaModelEntityModel, forceInit?: boolean, useScale?: boolean) {
      if (caps.cfg || forceInit === true) {
        let validators;
        if (this.attrValidatorFn.has(caps.self)) {
          if (!this.feqAttrValidatorFn.has(caps.self)) {
            //cache the attribute that be used twice, the map will be much more smaller
            this.feqAttrValidatorFn.set(caps.self,this.attrValidatorFn.get(caps.self));
          }
          validators = this.feqAttrValidatorFn.get(caps.self);
        } else {
          validators = this.createValidatorsByCaps(caps, formControl, false, forceInit, useScale);
          /** union type attr which already exists becomes required  */
          if (formControl.value && caps.isUnion()) { validators.push(Validators.required); }
          this.attrValidatorFn.set(caps.self, validators);
        }
        formControl.setValidators(validators);
        formControl.enable({ onlySelf: false, emitEvent: false });

2. Formcontrol's onlySelf , true only update itself, not its parent

       formControl.enable({onlySelf:false,emitEvent:false});

---> formControl.enable({onlySelf:true,emitEvent:false});

Change to true, time consume of each control is drop to 0.0xxms from 500ms

3. Update each formcontrol will fire the formArray's distinct validator, only need to run one time when all formcontrol are stable

  private formArrayInit(formArray: FormArray, caps: MetaModelEntityModel, forceInit?: boolean, useScale?: boolean) {
    formArray.setValidators(createDistinctElementsValidator());

    for (const element of formArray.controls) {
      if (element instanceof FormControl) {
        // is a FormControl
        console.log(element, formArray);
        this.formArrayControlInit(caps, element, true, useScale);
      } else if (element instanceof FormGroup) {
        // is a FormGroup
        if (caps.attrs !== undefined) {
          const formGroup: FormGroup = <FormGroup>element;
          this.formGroupInitByCapsImpl(formGroup, caps, forceInit, useScale);
        }
      } else if (element instanceof FormArray) {
        // FIXME: dostolski add handling of FormArray inside formArray
      }
    }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值