FormGroup 2 Api
Manage relationships between a form group mega model and a REST endpoint. When this method is called, it will automatically determine which models in the form group need to be saved and where to save them. Supports POST/PUT/PATCH/DELETE.
Imagine that you have a formgroup with the following structure:
And then a REST api with full CRUD operations for each model similar to the following:
In a large non-linear form figuring out what data has changed and where it needs to go is a huge task. By defining the relationship between the form group and the api we can automate all this interaction.
Key Features
- Recursive: Supports complex deeply nested form models. Is smart enough to dig through arrays in objects in arrays etc
- All CRUD operations supported: The utility will check the dirty flag to see if any data in the model has changed. If so, it will check if a uniqueID has already been set. If yes, a PUT/PATCH will be performed, if no it will be POST. To perform a delete, set of property of '$$deleted' to true before the utility runs.
- Handle API responses automatically: Responses from the api are patched into the existing model (useful for things like adding in the uniqueID on POST) and the model will be removed from the form group on a successful DELETE.
- Resets automatically: After a successfull api call, all models that have been modified have have their dirty flags reset to reset up for future saves
- HTTP requests run in parallel: All changed models have their http calls run in parallel as a single subscription
- Supports changed props only By default it will only send the deltas of any changed models
1. Create the form group
For larger models that start as JSON it's convenient to use the jsonToFormGroup utility function found in @ntersol/forms. Otherwise just use form builder.
2. Setup
Import the FormGroup2ApiModel interface and use that to create the model that the "saveFormGroup" function will use
3. Perform Save
Import the "saveFormGroup" function, supply the angular http service, the formGroupModel that was just created above and then the form group.