json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

Why does the schemaFormRedraw use angular.copy() ? #799

Closed Lorless closed 7 years ago

Lorless commented 8 years ago

Why does the schemaFormRedraw use angular.copy(scope.initialForm) ? This causes a lot of issues where my variables and watchers detach themselves from the form object I was watching. I modified the library to not use using angular.copy() and it worked fine.

Is it really necessary to call the copying function when the $on('schemaFormRedraw') watcher calls the render(schema, form) function anyway?

Sorry for noob question/ issue structure, first ever issue, I did read the support docs! I dont know how to label this a question either...

Lorless commented 7 years ago

It would be nice to get an answer to this. Is there any point at all for the use of the copy? It completely destroys the ability to save the state of any variables outside of the model scope since it detaches form and schema from their original copies. Very difficult to work around and seemingly doesn't have any benefits.

Anthropic commented 7 years ago

@Lorless we modify the data within our library, if we used the source object then it would be modified which can cause all sorts of issues. If you search the issue list for closed issues with 'angular.copy' you will find all the previous issues reporting problems caused by that behaviour.

Please re-open if you do not feel the question has been answered sufficiently.

Lorless commented 7 years ago

@Anthropic Hi Anthropic, this problem keeps rearing its head at us. The core problem here is how do we maintain the state of the form as we change it? We need to be able to save the state of variables that are on the form. The copy detaches our directives from the form where all our useful options are.

We will also need to insert fields via AJAX requests to get JSON files with the fields we want. Is this even possible? I can see that appendToArray() has the ability to modify the form and keep its state on a redraw.

This issue is related, schema form needs some way to be able to save state without resorting to editing the model.

526

Anthropic commented 7 years ago

@Lorless yes state awareness is beneficial. You can always add fields to the form definition that do not need to be a part of the schema and track with those, but state is something I do want myself.

In regard to directives, not sure what you mean, I'm not sure why you would pass directives in to schema-form as they are not valid in a json-schema or in the form definition. You can apply css to container or field that can trigger and attach directives where needed. You can also watch the model object and track changes to it. Are you talking about a custom add-on?

Lorless commented 7 years ago

@Anthropic Hi again, in answer to your question, directives themselves aren't being passed into the form but they do exist inside my custom elements. Often these directives have no way to know what context they are in apart from the 'form' variable available in each form element. So we tend to save stuff onto those form elements. Ie {type:'customSelect', key: 'object.name', isActive: false}. We then use a directive that modifies the value of isActive.

The behaviour i have noticed is that the very first schemaFormRedraw event you broadcast resets the fields on the form. So where isActive had been changed to true it is now false again. Any more calls of schemaFormRedraw however do not have the same effect.

Is this behaviour you would expect to see?

This seems like a bug to me. I don't understand why the first redraw would affect variables but subsequent redraws keep them the same. This is really difficult to account for. Im trying to avoid using redraw altogether since it causes so many problems with more dynamic forms but i just cant in this instance.

Anthropic commented 7 years ago

This may be easier once I add the ability to reference state data, not sure how long that will take though... but then the add-on could set the value in state and not be bothered by a redraw at all. Although it could probably handle that within its own code anyway if you had a state provider.

Lorless commented 7 years ago

I've actually got a state provider service already where I'm keeping track of things. I wanted to see whether it was possible to avoid using it as it can be a bit cumbersome. I'll use it for now. Thanks for getting back to me.