Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
By-passing $dirty check
Message
De
02/04/2015 03:20:13
 
 
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01609780
Message ID:
01617680
Vues:
37
This message has been marked as a message which has helped to the initial question of the thread.
>>>>>>Think that may be overkill - you'll be setting form to pristine even if other inputs are dirty.
>>>>>>
>>>>>>>I think I got it to work, but I am continuing tests now. Here is my current code for the directive, let me know if you see problems:
>>>>>>>
>>>>>
>>>>>Do you think saving current form's state in focus event is not going to work? Do you see my variable for saving current form's state?
>>>>Quick reply so I may have missed something but if the form is NOT dirty there's no point in setting it to pristine. And, as before, if it IS dirty then that may be because an input other than the one you are interested in is dirty......
>>>>
>>>So, in the focus event we save current form's status. If it's dirty, it means some other control set it dirty, so we will not reset the form. If it's false (e.g. form was in pristine state), then we need to reset it.
>>>
>>>That's my current logic.
>>
>>OK. Missed that - sounds OK....
>
>I did some extra changes and this is my current directive with some logging info to see what's going on:
>
>
>(function () {
>    'use strict';
>
>    var app = angular.module('sysMgrApp');
>
>    app.directive('noDirtyCheck', [function () {
>        // Interacting with input elements having this directive won't cause the
>        // form to be marked dirty.
>        // http://stackoverflow.com/questions/17089090/prevent-input-from-setting-form-dirty-angularjs
>        return {
>            restrict: 'A',           
>            require: ['^form', '^ngModel'],
>
>            link: function (scope, element, attrs, controllers) {
>                var form = controllers[0];
>                
>                var currentControl = controllers[1];
>
>                var formDirtyState = false;
>
>                var manualFocus = false;
>
>                element.bind('focus',function () {
>                    manualFocus = true;
>                    if (form) {                        
>                        window.console && console.log('Saving current form ' + form.$name + ' dirty status: ' + form.$dirty);
>                        formDirtyState = form.$dirty; // save form's dirty state
>                    }
>                 });
>                
>                element.bind('blur', function () {
>                    if (currentControl) {
>                        window.console && console.log('Resetting current control ' + currentControl.$name + ' dirty status to false (called from blur)');
>                        currentControl.$dirty = false; // Remove dirty state but keep the value
>                        if (!formDirtyState && form && manualFocus)
>                            form.$setPristine();
>                        
>                        manualFocus = false;
>                        //          scope.$apply();
>                    }
>                });
>            }
>        };
>    }]);
>})();
>
>I've been testing and it seems to now work OK in that invoices edit form that prompted me looking into the issue. The first level form (Accounts Edit) do not seem to show any prompts right now, though. And this is even if I don't go to the tabs that have this noDirtyCheck directive so I think this is a separate problem.
>
>In any case, I am tired of that work and will switch now to some other tasks.

Looks good. Capturing the form in the link phase is certainly a better idea than mine. But, off the top of my head, I don't see what purpose 'manualFocus' serves ?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform