2016-09-24 6 views
2

はじめ結合複合オブジェクト

私の目標は、私はアプリケーション全体で再利用できるようにアウレリアにカスタム要素を作成することです。

私は意志がオペレータに関する情報を保持し、私の計画はいくつかでそれを再利用することである オペレータ詳細オペレータdetail.htmlオペレータdetail.js)と呼ばれるコンポーネントを作成しました。この文脈では

アプリケーションの場所。このユースケースで

Iは、operatorDetailslegalRepresentativeへの参照を保持electornicRegistrationFormオブジェクトを持っています。両方のインスタンスは、のelectornicRegistrationFormモジュールに注入され、後で印刷される文書を作成するためのウィザードの一部として使用されます。

このelectronicRegistraionFormは、operatorstepコンポーネントに注入されます。私はoperatorStep.htmlに含まれており、そのコンポーネントを確認しました

オペレータdetail.htmlコンポーネントが正しくレンダリングされています。オブジェクトオペレータの値がIN(バインドさ)が表示されるようにオペレータdetail.html成分にoperatorStep.jsから(バインド)プロパティオペレータを渡す方法

通報

二者拘束力のある方法。次の例では

は、値からthis.operator.firstName「演算子のステップからの最初の名前は」オペレータdetail.htmlコンポーネントに表示されません。

ソースコード

electronicRegistrationForm.js

import { OperatorDetail } from 'common/operator-detail'; 
import { LegalRepresentative } from 'common/legalRepresentative'; 
import { inject } from 'aurelia-framework'; 
@inject(OperatorDetail, LegalRepresentative) 
export class ElectronicRegistrationForm { 
    constructor(operatorDetail, legalRepresentative) { 
    this.operator = operatorDetail; 
    this.legalRepresentative = legalRepresentative; 
    } 
} 

オペレータdetail.js

import {inject, NewInstance} from 'aurelia-framework'; 
import {ValidationRules, ValidationController} from 'aurelia-validation'; 

@inject(NewInstance.of(ValidationController)) 
export class OperatorDetail { 
    constructor(validationController) { 
    this.validationController = validationController; 
    this.firstName = ''; 
    } 

    attached() { 
    ValidationRules 
     .ensure('firstName').displayName('Ime').required() 
     .on(this); 
    }   
} 

オペレータdetail.html

<template bindable="operatorvalue"> 
    <div> 
    <div class="row"> 
     <div class="col-sm-6"> 
     <div class="form-group"> 
      <label for="firstName" t='first_name'></label> 
      <input type="text" class="form-control" id="firstName" value.bind="operatorvalue.firstName "> 
     </div> 
     </div>   
    </div> 
</template> 

operatorStep.js

import { ElectronicRegistrationForm } from 'model/electronicRegistrationForm'; 
import { inject, NewInstance } from 'aurelia-framework'; 
import { RegistrationWizard } from 'registration/registrationWizard'; 
import { WizardStep } from 'registrationSteps/wizardStep'; 
import { ValidationController } from 'aurelia-validation'; 
import {bindable, bindingMode} from 'aurelia-framework'; 

@inject(ElectronicRegistrationForm, RegistrationWizard, NewInstance.of(ValidationController)) 
export class OperatorStep extends WizardStep { 
    @bindable({ defaultBindingMode: bindingMode.twoWay }) operator; 

    constructor(electronicRegistrationForm, regWiz, validationController) { 
    super(electronicRegistrationForm, regWiz, validationController); 
    this.operator = electronicRegistrationForm.operator; 
    this.operator.firstName='First name from operator step'; 
    this.representative = electronicRegistrationForm.legalRepresentative; 
    } 
} 

operatorStep。あなたはViewModelにせずにビューを持っている場合、HTMLテンプレートにバインド可能なプロパティを宣言

<template> 
    <require from="common/operator-detail"></require> 
    <form validation-renderer="bootstrap-form"> 
    <operator-detail operatorvalue.bind="operator"></operator-detail> 
    </form> 
</template> 

答えて

2

がためです。

operator-detail.htmlbindable="operatorvalue"は、この要素にViewModelも定義されているため動作しません。あなたはこのようにそれを維持したい場合は、単にテンプレートからbindable="operatorvalue"を削除し、代わりにそのようなあなたのViewModelにそれを宣言:

import {inject, NewInstance, bindable} from 'aurelia-framework'; 
import {ValidationRules, ValidationController} from 'aurelia-validation'; 

@inject(NewInstance.of(ValidationController)) 
export class OperatorDetail { 
    @bindable operatorvalue; 

    constructor(validationController) { 
     this.validationController = validationController; 
     this.firstName = ''; 
    } 

    attached() { 
     ValidationRules 
      .ensure('firstName').displayName('Ime').required() 
      .on(this); 
    }   
} 

オペレータdetail.htmlはその後になる:

<template> 
    <div class="row"> 
     <div class="col-sm-6"> 
      <div class="form-group"> 
       <label for="firstName" t='first_name'></label> 
       <input type="text" class="form-control" id="firstName" value.bind="operatorvalue.firstName "> 
      </div> 
     </div> 
    </div> 
</template> 

また、operator-detail.jsを削除し、検証用のものを別の場所に置くだけで、テンプレート内のバインド可能なプロパティ宣言を機能させることができます。次に、OperatorDetailを登録フォームに挿入するのではなく、オペレータオブジェクトを新規に作成することができます。

両方の方法で動作します。

ViewModelsを使用しないで作業するということは、検証ロジックをelectronicRegistrationForm.jsに入れるということです。

ViewModelsを使用するとより多くのカプセル化ができます。これは、ビュー固有のロジックがここに示したものよりも複雑な場合に一般的に好まれます。

EDIT

Google Chromeを使用している場合は、私は非常にaurelia context拡張機能を使用することをお勧めします。その後、HTMLを検査し、オペレータオペレータステップで定義されていたが、 operatorvalue オペレータ詳細で未定義たことを見ることができました。これにより、のoperatorvalueバインド可能な宣言が、他のものではなく、間違っていなければならないことがわかります。