Skip to content
This repository has been archived by the owner on Feb 22, 2019. It is now read-only.

cdo.form_and_model_generators

Pavel Vlasov edited this page Feb 22, 2015 · 16 revisions

CDO JavaScript API

org.nasdanika.cdo bundle provides several classes for generating Bootstrap/AngularJS forms and models from EClass and EOperation metadata and annotations:

  • FormGeneratorBase - Abstract base class. It has a number of protected methods which can be overriden by subclasses to fine-tune form generation. Default method implementations use metadata such as feature/parameter name and type and annotations to generate forms.

Annotations

Annotations used by the generators are:

  • org.nasdanika.cdo.web.html.form-control for EClass features - attributes and references (by default controls are not generated for references) and for EOperation parameters. This annotation supports the following details keys:
    • attribute:<attribute name> - Allows to specify control attribute.
    • group-attribute:<attribute name> - Allows to specify group attribute.
    • control-id - Control ID, defaults to <feature|parameter name>_control.
    • default - Default value. If not set then attribute default value is used for attributes.
    • help-text - Help text. No default. In AngularJS generators help text is used to display control-level validation messages.
      • inline - Inline checkbox control if set to true.
    • input-type - One of InputType values. Default value is computed from feature/parameter type by getInputType() methods which can be overriden.
    • label - Control label. Defaults to feature/parameter name split by camel case with the first word capitalized and the rest uncapitalized. E.g. a label for userName parameter or feature would be "User name".
    • placeholder - Control placeholder for controls which support placeholders. Default is the same as for label.
    • private - If true then feature/parameter is not included into the generated form.
    • required - Marks the generated control as required if set to true.
    • style:<style name> - Allows to customize control style. E.g. style:background -> yellow
    • group-style:<style name> - Allows to customize group style.
    • validator - Control validator used by AngularJS generators. The value of the validator details key shall be a JavaScript function body returning validation message or a promise for a validation message. If the return value is falsey, e.g. undefined or an empty string, then validation is successful, otherwise the return value is displayed as a control-level error message. The function body has access to the control value through value parameter and to the whole model through this.
  • org.nasdanika.cdo.web.html.form for EClasses and EOperations. This annotation supports the following details keys:
    • model - Object declarations to add to the model definition, e.g. helper functions.
    • validator is used by AngularJS generators in generation of validate() model function. The value of the validator details key shall be a JavaScript function body returning validation message or a promise for a validation message. If the return value is falsey, e.g. undefined or an empty string, then validation is successful, otherwise the return value is displayed as a form-level error message. The function body has access to the model data through value parameter and to the whole model through this.

AngularJS model

AngularJS generators have generateModel() method which returns JavaScript object definition with the following entries:

  • data - an object which is either empty or contains default values for features/parameters. Form controls are bound to this object's entries. For existing object replace the generated data entry with the JavaScript API object.
  • validationResults - an empty object to hold validation messages for controls.
  • validate() - a function which invokes validators for controls and the form and returns a promise of boolean value. If the promise is resolved with true then the form is valid.
  • apply(target) - this function is generated by AngularJsEOperationFormGenerator. It invokes the target passing model data as arguments in the order in which they are defined in the ECore model. If target is an object with <operation name> function, then that function is invoked, otherwise target assumed to be a function to be invoked.
  • validateAndApply(target) - this function is generated by AngularJsEOperationFormGenerator. It invokes validate(). If validation is successful, then it invokes apply(target). validateAndApply returns a promise resolved with return value of apply() or rejected with { validationFailed: true } if validation failed, or { targetInvocationError: <target rejection reason> }. This function expects target function to return a promise, which is the case for JavaScript API functions generated for model objects' EOperations.
Clone this wiki locally