Sum*_*ndu 7 javascript json aurelia aurelia-binding
我正在使用Aurelia基于json构建动态表单.表单是从json生成的,如下所示:
Schema = [{
'key': 'Name',
'display': 'Name',
'type': 'text',
'placeholder': 'Name',
'required': true
},
{
'key': 'IsSubscribed',
'display': 'Subscribed to newsletter?',
'type': 'checkbox',
'placeholder': null,
'required': false
}];
Run Code Online (Sandbox Code Playgroud)
填写表单的模型可通过Web API服务获得.所以,我成功使用以下模板.
<template>
<section class="au-animate">
<h2>Edit Form</h2>
<form class="form-group">
<div repeat.for="item of Schema" class="form-group">
<label if.bind="item.type === 'text' || item.type === 'checkbox'" class="control-label" for.bind="item.key">${item.display}
<input class="form-control" id.bind="item.key" placeholder.bind="item.placeholder" type.bind="item.type" value.bind="Model[item.key]" />
</label>
<label if.bind="item.type === 'textarea'">${item.display}
<textarea placeholder.bind="item.placeholder" value.bind="Model[item.key]></textarea>
</label>
...
</div>
</form>
</section>
</template>
Run Code Online (Sandbox Code Playgroud)
当模型包含另一个对象作为属性时,我现在面临困难.例如,对于属性地址我想要一个City的输入框.
因此,item.key = "Address.City".
我可以绑定(1)Model.Address.City或(2)Model ['Address'] ['City']这是不可能的,因为表单是在运行时生成的.我想用类似的东西(3)型号["Address.City"],这样我就可以用模型[item.key]的结合.有没有简单的语法来实现这一目标?
Angular Js中类似应用的示例是Angular Schema Form
提前致谢.
这可以通过一种能够理解如何处理键的绑定行为来实现.最终结果是绑定将像任何其他绑定表达式一样起作用.
这是一个例子:https://gist.run?id = 720d20b2db5adba92f62f7e665cf3b96
app.html
<template>
<require from="./dynamic-expression-binding-behavior"></require>
<label>
Address 1:
<input value.bind="model & dynamicExpression:'address.address1'">
</label>
<label>
Address 2:
<input value.bind="model & dynamicExpression:'address.address2'">
</label>
<label>
City:
<input value.bind="model & dynamicExpression:key">
</label>
<label>
State:
<input value.bind="model & dynamicExpression:'address.state'">
</label>
<label>
Zip:
<input value.bind="model & dynamicExpression:'address.zip'">
</label>
</template>
Run Code Online (Sandbox Code Playgroud)
app.js
export class App {
model = {
address: {
address1: '1 Main Street',
address2: '',
city: 'Burlington',
state: 'VT',
zip: '05401'
}
};
key = 'address.city';
}
Run Code Online (Sandbox Code Playgroud)
动态表达结合-behavior.js
import {inject} from 'aurelia-dependency-injection';
import {Parser} from 'aurelia-binding';
import {rebaseExpression} from './expression-rebaser';
@inject(Parser)
export class DynamicExpressionBindingBehavior {
constructor(parser) {
this.parser = parser;
}
bind(binding, source, rawExpression) {
// Parse the expression that was passed as a string argument to
// the binding behavior.
let expression = this.parser.parse(rawExpression);
// Rebase the expression
expression = rebaseExpression(expression, binding.sourceExpression);
// Squirrel away the binding's original expression so we can restore
// the binding to it's initial state later.
binding.originalSourceExpression = binding.sourceExpression;
// Replace the binding's expression.
binding.sourceExpression = expression;
}
unbind(binding, source) {
// Restore the binding to it's initial state.
binding.sourceExpression = binding.originalSourceExpression;
binding.originalSourceExpression = null;
}
}
Run Code Online (Sandbox Code Playgroud)