Form.reset () without running onChange

I know that you can prevent onChange from starting when setting the value of dijit elements (i.e. textbox.set('value',value,false) ). Is there a way to do this when resetting dijit.form.Form ( form.reset() )?

My problem is trying to track the dirty shape (i.e. the value of the text box has been changed). I am capturing onChange events for the controls in the form. The problem is that form.reset () dispatchesChange events for each control that received a reset back to the initial state of the control. This way form.reset() set my dirty flag, as each reset control triggers onChange.

I saw possible solutions that include resetting control forms using control by setting the value of _lastValueReported and then setting the value of the control, but there seems to be a simpler solution.

If the form had an isDirty() method, that would be ideal.

Or is there a better way to track when a form is dirty?

+4
source share
1 answer

It is not possible to prevent the onChange event from firing when the reset() function is called at the moment, looking at the code , what happens when you use dijit/form/Form::reset() , it is that it will call the reset() function for each child widget of this form:

 reset: function(){ array.forEach(this._getDescendantFormWidgets(), function(widget){ if(widget.reset){ widget.reset(); } }); }, 

The reset function (at least all form widgets inherited from _dijit/form/_FormValueMixin ) is implemented as follows:

 reset: function(){ this._hasBeenBlurred = false; this._setValueAttr(this._resetValue, true); } 

So, as you can see, it just calls _setValueAttr() with the second argument (which raises the change event).


Thus, the only way to change this behavior is to implement it yourself by overriding the necessary methods. For instance:

 var MyForm = declare("my/Form", [ Form ], { reset: function(priorityChange) { array.forEach(this._getDescendantFormWidgets(), function(widget){ if(widget.reset) { widget.reset(priorityChange); } }); } }); var myTextBox = declare("my/TextBox", [ TextBox ], { reset: function(priorityChange) { this._hasBeenBlurred = false; this._setValueAttr(this._resetValue, priorityChange); } }); 

This allows you to add the priorityChange parameter to the reset() function, which allows you to fire the event (or not).

A full example can be found in JSFiddle: http://jsfiddle.net/f954z/


If you do not want to override the reset() function of all form widgets (I just did dijit/form/Form and dijit/form/TextBox ), then the best thing you can do is open the ticket and hope that it will be implemented.

0
source

Source: https://habr.com/ru/post/1494517/


All Articles