Permalink
Browse files

feat(project): more flexibly set the options in schema

  • Loading branch information...
bas080
bas080 committed Jul 19, 2016
1 parent abfd966 commit d75a56365db4b41f59880b7e47e4985eb2d7bd0b
@@ -3,6 +3,7 @@ import {Config} from './config';
import {Config as ViewManagerConfig} from 'aurelia-view-manager';
export {entitySchema} from './entity-schema';
export {Form} from './form';
export * from './utils';

export function configure(aurelia, configCallback) {
aurelia.aurelia.use.plugin('aurelia-view-manager');
@@ -24,6 +25,9 @@ export function configure(aurelia, configCallback) {
actions : '{{framepath}}/actions',
collection : '{{framepath}}/collection',
conditional : '{{framepath}}/conditional',
select : '{{framepath}}/select',
radios : '{{framepath}}/radios',
checkboxes : '{{framepath}}/checkboxes',

/* all input components */
button : '{{framepath}}/input.html',
@@ -7,6 +7,6 @@
<!-- has a view model this is a component with view model -->
<compose
if.bind="hasViewModel && visible"
model.bind="{element: element, value: value, message: message}"
model.bind="model"
view-model.bind="view"></compose>
</template>
@@ -18,6 +18,7 @@ export class FormField {
constructor(config, viewManager) {
this.config = config;
this.viewManager = viewManager;
this.model = this;
}

attached() {
@@ -2,7 +2,7 @@
<require from="./form-group"></require>
<require from="./../../../attributes"></require>
<form-group element.bind="element" attributes.bind="element.attributes">
<div class="checkbox" repeat.for="option of element.options">
<div class="checkbox" repeat.for="option of options">
<label>
<input
attributes.bind="option.attributes"
@@ -0,0 +1,3 @@
import {Options} from '../options';

export class CheckboxesElement extends Options {}
@@ -2,7 +2,7 @@
<require from="./../../../attributes"></require>
<require from="./form-group"></require>
<form-group element.bind="element" message.bind="message">
<div class="radio" repeat.for="option of element.options">
<div class="radio" repeat.for="option of options">
<label>
<input
attributes.bind="element.attributes"
@@ -0,0 +1,3 @@
import {Options} from '../options';

export class RadiosElement extends Options {}
@@ -6,7 +6,7 @@
attributes.bind="element.attributes"
value.bind="value"
class="form-control">
<option repeat.for="option of element.options"
<option repeat.for="option of options"
attributes.bind="option.attributes"
name.bind="element.key"
model.bind="option.value">${option.name}</option>
@@ -0,0 +1,3 @@
import {Options} from '../options';

export class SelectElement extends Options { }
@@ -0,0 +1,32 @@
import {normalizeOptions} from '../../utils';
import {logger} from '../../aurelia-form';
import {BindingEngine, inject} from 'aurelia-framework';

@inject(BindingEngine)
export class Options {

constructor(bindingEngine) {
this.bindingEngine = bindingEngine;
}

activate(model) {
if (!Array.isArray(model.element.options)) {
logger.error('element \'s options should be an array');
}

this.value = model.value;
this.element = model.element;
this.options = normalizeOptions(model.element.options);

/* when options change they have to be renormalized */
this.optionsObserver = this.bindingEngine.collectionObserver(model.element.options)
.subscribe(() => {
this.options = normalizeOptions(model.element.options);
});
}

unbind() {
this.optionsObserver.dispose();
}

}
@@ -0,0 +1,21 @@
export function normalizeOptions(options) {
return options.map(option => {
if (typeof option === 'string') {
return {
name: option,
value: option
};
}

if (option.name && option.value) {
return option;
}

if (option.name || option.value) {
return {
name: option.name || option.value,
value: option.name || option.value
};
}
});
}

0 comments on commit d75a563

Please sign in to comment.