gpt4 book ai didi

forms - AureliaJS - 从父调用子函数

转载 作者:行者123 更新时间:2023-12-05 01:24:56 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com