七段

无论怎样,请让我先感谢一下国家。

BlogJava 首页 新随笔 联系 聚合 管理
  35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks
  (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");
            })
posted on 2009-12-08 11:28 sevenduan 阅读(319) 评论(0)  编辑  收藏 所属分类: JavaScript

只有注册用户登录后才能发表评论。


网站导航: