Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$render stopped working at angular 1.2.2 (file validation directive)

I have migrated from angular 1.0.8 to angular 1.2.2 yesterday, and beside a bunch of other things that got broken and I've already fixed, the $render function on the following directive is not firing anymore.

Did anyone encouter such a behavior before?0

directive('validFile', function (utils, $filter) {
    return {
        require: 'ngModel',
    link: function (scope, el, attrs, ngModel) {

      if(utils.isMobileAgent())
          return;
      var form = el.parents().find('form');
        ngModel.$render = function () {
          debugger;
          if(form.hasClass('ng-pristine'))
            return;

          if(el.val() && el.val().length > 0){
            ngModel.$setViewValue(el.val());
          }

          if(el.hasClass('ng-invalid')){
            el.parent().addClass('ng-invalid').addClass('ng-invalid-required');
            ngModel.$setValidity(attrs.name, false);
            ngModel.$setPristine(attrs.name, false);
            scope.fileMsg =  $filter('translate')('PLEASESELECT') + ' ' + $filter('translate')(attrs.name);
            // scope.layout.showFileError = true;
          }
          else{
            el.parent().removeClass('ng-invalid').removeClass('ng-invalid-required').addClass('ng-valid');
            ngModel.$setValidity(attrs.name, true);
          }
        };
        el.bind('mouseover', function(){
          if(form.hasClass('ng-dirty') && el.parent().hasClass('ng-invalid'))
          el.removeClass('ng-pristine');
        });
        el.bind('mouseleave', function(){
          if(el.val() && el.val().length > 0){
           el.addClass('ng-pristine');
          }
        })
        el.bind('change', function () {
            scope.$apply(function () {
                ngModel.$render();
            });
        });
        form.bind('change', function () {
            scope.$apply(function () {
                ngModel.$render();
            });
        });
    }
};
});

markup:

<input type="file" data-ng-model='model.formData.resume' name="resume" data-valid-file data-my-validate data-value-required="true">
like image 536
Oleg Belousov Avatar asked Nov 28 '13 08:11

Oleg Belousov


2 Answers

Increase the priority of your directive to something above 0.

For example:

myApp.directive('validFile', function ($filter) {
   return {
     priority: 10,

Here's a detailed explanation of the problem that I found associated with this ui-tinymce issue that's, at it's root, the same as yours.

The short version of the explanation being that this change causes input's $render to take precedence over your own. By bumping the priority of your directive you, in effect, give your $render priority- as it was before that change in 1.2 rc3.

like image 165
KayakDave Avatar answered Nov 02 '22 03:11

KayakDave


I've tested this with angular 1.2.10 for a textbox and whatever priority I set, original input $render method was set afterwards overriding my $render function.

This problem occurs in angular-ui tinymce module as well which can not render the initial model value. So I changed the timeout part in tinymce directive to override the original $render method as follows:

var render = function() { // my rendering code }    
setTimeout(function () {
                      tinymce.init(options);
                      if (ngModel.$render != render) {
                          var originalRender = ngModel.$render;
                          ngModel.$render = function() {
                              originalRender();
                              render();
                          };
                  }
              });

This way, after all "link" functions are executed, you can override the render method.

like image 43
Cagatay Kalan Avatar answered Nov 02 '22 03:11

Cagatay Kalan