• javascript设计模式(一)职责链模式China of responsibility


    表单请求搜索,验证,提示。表单提示验证请求模块

    //请求模块
        var sendData=function(data,dealType,dom){
            var xhr=new XMLHttpRequest();
            var url='getData.php?mod=userInfo';
            xhr.onload=function(event){
                if((xhr.status>=200 && xhr.status<300)|| xhr.status==304){
                    dealData(xhr.responseText,dealType,dom)
                }else{
    //                请求失败
                }
                //拼接请求字符串
                for(var i in data){
                    url+="&"+i+'='+data[i];
                }
                //发送请求
                xhr.open('get',url,true);
                xhr.send(null)
            }
        }
        //响应数据适配模块
        var dealData=function(data,dealType,dom){
            var dealType=Object.prototype.toString.call(data);
            //判断相应的数据处理对象
            switch (dealType){
                case 'sug':
                    if(dealType==='[object Array]'){
                        return createSug(data,dom);
                    }
                    if(dealType==="[object Object]"){
                        var newData=[];
                        for(var i in data){
                            newData.push(data[i])
                        }
                        return createSug(newData,dom);
                    }
                    return createSug([data],dom);
                break;
                case "validata":
                    return createValidataResult(data,dom);
                break;
            }
        }
        //创建组件模块
        //提示框组件
        var createSug=function(data,dom){
            var i= 0,
                len=data.length
                html="";
            for(;i<len;i++){
                html+="<li>"+data[i]+"</li>";
            }
            dom.parentNode.getElementsByTagName("ul")[0].innerHTML=html;
        }
        //校验组件
        var createValidataResult=function(data,dom){
            dom.parentNode.getElementsByTagName("span")[0].innerHTML=data;
        }
    

    将复杂的功能拆分成单个独立的对象

  • 相关阅读:
    毕业了!
    mesos无执行器启动docker
    docker run option
    maintenance
    virtualenv
    multi role
    排序之插入排序
    DLU-1064 美声葱
    codeforces-1025 A Doggo Recoloring
    codeforces-1027 C Minimum Value Rectangle
  • 原文地址:https://www.cnblogs.com/dangou/p/7421991.html
Copyright © 2020-2023  润新知