JavaScript RemoteValidator for AJAX(Draft)

JavaScript RemoteValidator for AJAX(Draft)
  ( function (){
                
var  uuid  =   0 ;
                
var  NEW  =   0 , PENDING  =   1 , FINISH  =   2 ;
                
var  RemoteRule  =  window.RemoteRule  =   function (fn, options){
                    
this .id  =  uuid ++ ;
                    
this .fn  =  fn;
                    
this .para  =  options.requestPara;
                    
this .showTips  =   function (){
                        options.showTips();
                    }
                }
                
                
var  RemoteValidator  =  window.RemoteValidator  =   function (){
                    
this .rules  =  {};
                    
this .status  =  {};
                }
                RemoteValidator.prototype 
=  {
                    addRule: 
function (rule){
                        
this .rules[rule.id]  =  rule;
                        
this .status[rule.id]  =  NEW;
                    },
                    reset: 
function (){
                        
this .rules  =  {};
                        
this .status  =  {};
                    },
                    validate: 
function (callBack){
                        
var  self  =   this ;
                        
for  ( var  id  in  self.rules) {
                            
var  rule  =  self.rules[id];
                            
var  updateFn  =  ( function (){
                                
return   function (data){
                                    
if  (data) {
                                        
delete  self.status[rule.id];
                                    }
                                    
else  {
                                        self.hasError 
=   true ;
                                    }
                                    
if  (self.hasError) {
                                        rule.showTips();
                                    }
                                    
var  isEmpty  =   true ;
                                    
for  ( var  id  in  self.status) {
                                        isEmpty 
=   false ;
                                        
break ;
                                    }
                                    
if  (isEmpty) {
                                        callBack();
                                    }
                                }
                            })();
                            self.status[rule.id] 
=  PENDING;
                            rule.fn(rule.para, updateFn);
                            
                        }
                    }
                }
                
            })();
            
            
var  dwrFnMock  =   function (para, callBack){
                setTimeout(
function (){
                    
if  (para.value  >   0
                        callBack(
true );
                    
else  
                        callBack(
false );
                }, 
1000 );
            };
            
var  validator1  =   new  RemoteValidator();
            validator1.addRule(
new  RemoteRule(dwrFnMock, {
                requestPara: {
                    value: 
1
                },
                showTips: 
function (){
                    alert(
" hasError! " );
                }
            }));
            validator1.validate(
function (){
                alert(
" submit " );
            });
            
var  validator2  =   new  RemoteValidator();
            validator2.addRule(
new  RemoteRule(dwrFnMock, {
                requestPara: {
                    value: 
- 1
                },
                showTips: 
function (){
                    alert(
" hasError! " );
                }
            }));
            validator2.validate(
function (){
                alert(
" submit " );
            })

你可能感兴趣的:(JavaScript RemoteValidator for AJAX(Draft))