forms - AureliaJS - 从父调用子函数

标签 forms aurelia aurelia-templating

我正在使用 AureliaJS 构建一个动态表单场景,其中我有一个包含所需总操作的父表单和多个子表单,它们会根据用户输入进行更改。

这些 child 的形式本身只有两个特定的东西。他们的模型及其模型的验证规则。

所以我的问题是,父表单如何调用当前子表单的验证规则?从 child 那里我知道可以调用 parent 的 View 模型。但是,如何从父级调用子级的任何函数?

场景类似于拥有一个基类,它有一个方法,这个方法可以覆盖子类。

有什么建议吗?如果需要,我很乐意改变方法。

这是一个例子:https://gist.run?id=1865041a15af60600cb7b538018bdccd

app.html

<template>
  <span>This is an APP</span>
  </p>
  <compose view-model.bind="'parentForm'"></compose>
</template>

app.js

import { autoinject } from 'aurelia-framework';

@autoinject
export class App {

}

childForm1.html

<template>
  <label> Price : </label>
  <input value.bind="model.data.price">
  <p/>
  <label> VAT : </label>
  <input value.bind="model.data.vat">
  <p/>
</template>

childForm1.js

import { autoinject } from 'aurelia-framework';

@autoinject
export class ChildForm1 {

  activate(model)
  {
    this.model = model;
  }

 validateRules (){

     if(this.model.data.price != '' && this.model.data.vat == '' )
      this.model.validateMessage = 'VAT is mandatory';
 }
}

childForm2.html

<template>
  <label>Address : </label>
  <input value.bind="model.data.address">
  <p/>
  <label>Phone : </label>
  <input value.bind="model.data.phone">
  <p/>
</template>

childForm2.js

import { autoinject } from 'aurelia-framework';

@autoinject
export class ChildForm2 {

  activate(model)
  {
    this.model = model;
  }

 validateRules (){

   if(this.model.data.phone != '' && this.model.data.address == '' )
      this.model.validateMessage = 'Address is mandatory';
 }
}

index.html

<!doctype html>
<html>
  <head>
    <title>Aurelia</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
    <script>
      require(['aurelia-bootstrapper']);
    </script>
  </body>
</html>

parentForm.html

<template>
  <button click.delegate="changeChildForm1()">Change Child Form 1</button>
  <button click.delegate="changeChildForm2()">Change Child Form 2</button>
  <p/>
  <p/>
  <form>
    <label>User : </label>
    <input value.bind="model.data.user">
    <p/>
    <compose view-model.bind="childFormVM" model.bind="model"></compose>
    <button click.delegate="save()">Save</button>
    <p/>
    <span> Validation Message :  ${model.validateMessage}</span>
  </form>
   <p/>
  <span>Price : ${model.data.price}</span><p/>
  <span>Vat : ${model.data.vat}</span><p/>
  <span>Phone : ${model.data.phone}</span><p/>
  <span>Address : ${model.data.address}</span><p/>
</template>

parentForm.js

import { autoinject } from 'aurelia-framework';

@autoinject
export class ParentForm {
  model = {
   validateMessage : '', 
   data : {
    user : 'My Name'
   }
 };

 childFormVM = 'childForm1';

 validateMessage = '';

 changeChildForm1() {

   this.childFormVM = 'childForm1';
 }

  changeChildForm2() {

   this.childFormVM = 'childForm2';
 }

 save(){
   this.validateRules();
   // How to call the validation rules from child ?
 }

 validateRules (){

   this.model.validateMessage = 'Validate by parent';
 }
}

最佳答案

将函数调用绑定(bind)到子项,以便您拥有从父项调用它的句柄。我通常更喜欢直接绑定(bind)子组件而不是使用 compose,但是您可以通过传递一个复杂的模型对象而不仅仅是模型,并将绑定(bind)函数作为模型属性。

父 View 模型:

class Parent {
  model = {};
  child1Validate = null;

  changeChildForm1() {
    if (typeof this.child1Validate === 'function') {
      // the binding was successful; proceed with function call
      let result = this.child1Validate();
      console.log(result);
    }
  }
}

父 View :

<my-child1 model="parentModel" go-validate="child1Validate"></my-child1>

subview 模型:

class MyChild1 {
  @bindable model;
  @bindable goValidate;
  bind() {
    // bind the child function to the parent that instantiates the child
    this.goValidate = this.runValidation.bind(this);
  }
  runValidation() {
    // do the validation and pass result to parent...
    return 'Success!';
  }
}

关于forms - AureliaJS - 从父调用子函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924342/

相关文章:

jquery - Emberjs-1.0.0-rc.6 点击动态添加更多或多个输入字段

Aurelia View 模型类命名

aurelia - 将复杂对象绑定(bind)到组件

aurelia - 在运行时更改元素类型

javascript - Aurelia - 默认情况下自定义元素不继承绑定(bind)上下文。可以吗?

javascript - 使用 HTML、CSS、JS 形式自动更新进度条

php - 表单回显成功而不向数据库提交任何内容。我的流量控制有问题吗?

javascript - PHP 表单动态输入

javascript - 通用 Aurelia 对话框需要在脏且有效时启用“保存”

javascript - 使用 <compose view-model ="./my-element"> 和 <my-element> 有什么区别?有哪些场景比较适合?