# Datasource
A datasource can be seen as a source of information and is typically a collection but does not always have to be.

Datasources are defined in the schema in a datasources array property.
Each datasource object must have a id of type number to identify itself uniquely.
The number sequancing is not important just that it is unique.

A datasource can have a name property, it is not used but helps human viewers to understand what the datasource represents.

Below are three examples of datasources.

```json
{
    "datasources": [
       {
          "id": 1,
          "name": "collectionField",
          "field": "model.collectionField"
       }, 
       {
          "id": 2,
          "name": "staticItems",
          "resource": [
            {
              "field1": "A",
              "field2": "A2"
            },
            {
              "field1": "B",
              "field2": "B2"
            }
          ]
       },
       {
          "id": 3,
          "name": "resource path",
          "remote": "/api/path"
       }
    ]
}
```

Datasources are often referenced by collection controls or collection properties on datasets.

The first example points to a field path on where the collection information can be found.
This is often used when the application populates these properties manually. 
The collection control will then bind to the field defined in the datasource.

The second datasource defines a static list of models defined as field name, value pairs.
This example can be either accessed by a collection control or by a dataset collection property.
The dataset collection property will be populated with an array of dataset objects for each of the resource items defined.
If a collection control accesses this directly it will attempt to populate the dom with defined templates bound to these resources.
This is a great way to limit what users see as it is always a static collection of values as defined in the resource. 

The third example defines a external resource that the dynamic forms do not understand.
It is up to the application to fetch these items


# Dataset
The dataset represents a model structure that contains data to bind against. Dataset is identified using an id.
To create a dataset from a schema definition use the dataset-factory, see the dataset-factory document for more details.
The convention is that the root model (name often model) has the id 0. This is not required but a good convention to set it appart. 
Fields are defined in the fields array property.

```json
{
  "datasets": [
    {
      "id": 0,
      "name": "model",
      "fields": [
        {
          "name": "field1"
        },
        {
          "name": "field2",
          "collection": true,
          "default": 1,
          "dataset": 1
        },
        {
          "name": "field3",
          "dataset": 2
        }
      ]
    }
  ]  
}
```

Dataset 0 gives us a dataset with a number of different field types.
The first (field1) is just a normal field, nothing special.

Field2 is a collection field. Collection fields can have a default property that points to the datasource to use for that collection but does not have to have one.
Collection fields can also have a dataset property. If it does have a dataset property the parent dataset will have a addField2 and removeField2 method that will add and remove datasets in the collection.
Note that identification in those methods are id properties so the model must have a id property.
Default fields here are usefull to populate the array with values and is often used in checkboxes, radio groups, comboboxes and details controls.
In the case of the details control you may want to add or remove items so in that case you may want to provide a dataset id.

Field3 shows a detail dataset, in these cases a dataset with the defined structure will be created and set as that property. 

The dataset can also have validations defined for each property. Note that this does not apply to either collection or dataset properties.

```json
{
  "datasets": [
    {
      "id": 0,
      "name": "model",
      "fields": [
        {
          "name": "field1",
          "validations": [
              {
                  "type": "default",
                  "rules": {
                      "required": true,
                      "readonly": "false",
                      "maxLength": 10,
                      "minLength": 5,
                      "max": 160,
                      "min": 20,
                      "pattern": "RegExExpression"
                  }
              }
          ]
        }
      ]
    }
  ]
}

```

As you can see validations is an array and in at some stage we will allow both default and custom validations.
The above field shows you all the type of rules we can define. Max and Min are only used for inputs where that makes sense e.g. numeric input.

## Dataset collection properties
When creating a dataset using the dataset factory, collection properties will initialize as a empty array. The dataset factory basically gives you a husk to work with.
To populate the dataset with values from your datasource, use the setInitialValues function on the dataset model. 

```js
this.model.setInitialValues(staffMember);
```

When you do this, any array properties on the dataset will also initialize if they have a default datasource defined.
Please note that if you do not define the array properties as part of this initialization the other functions to populate that array will no fire.

When initially creating the dataset, if a collection property has a defined dataset id a add and remove function will be added with that property name.  
Lets say the property name is "contacts", you will then have a add function called "addContacts" and a remove called "removeContacts".

When you connect a details control to a collection property you need to define the action property for the detail control.
Use the add function for the property you have defined.

```json
{
    "element": "details",
    "datasource": "model.details",
    "template": 1,
    "action": "model.addDetails"
}
```

If you do not define the action property as seen above, the add / remove toolbar at the bottom of the details control will not show.

Please note that on details controls the context for each item is the model in the collection that represents that item.
For binding you need to only define the property name. For example:

```json
{
    "element": "div",
    "content": "${code}"
}
```
