search
HomeWeb Front-endHTML TutorialApplicant page??Text input box and single-select multi-view reconstruction_html/css_WEB-ITnose

The recent school recruitment season, the internship unit provides school recruitment software services, there are many online users, and there are not too many new features to go online. Le Emperor is mainly responsible for doing some reconstruction. Think about the new colleagues who graduated this year and are already able to independently undertake business development. Leti has recently gained a deeper understanding of the MVC architecture, and his programming skills have also improved to a certain extent. He has learned a lot of front-end development specifications from his colleague Xinsheng. I would like to thank Xinsheng again for his patient teaching and selfless help.

The biggest difference between Ledi and Xinsheng is that Xinsheng has a profound theoretical foundation in handling and solving problems, that is, he knows why, and he is not just a programmer. Have your own thinking and know how to optimize code and performance. Le Di learned from Xinsheng his theoretical system and problem-solving methods. Quickly narrow the gap with the example of Xinsheng.

The view discussed in this article is under the applicant function in the unit, recruitment project. So why does it need to be refactored?

In this reconstruction work, I think there are two reasons:

  • The processing logic in the template is too much It is complex, does not comply with the separation of structure and processing logic, and the code is not highly readable.
  • <select data-name="<%=Name%>" data-obj="<%=controlData.Object%>" class="souce_name search_view width130">		<option value="">不限</option>							0){%>															<option value="<%=item.Value%>"></option>								<option value="<%=item.Value%>" selected></option>											<option value="<%=item.Value%>"></option>													<option value="<%=item.Value%>" title="<%=item.Text%>"></option>											<option value="<%=item.Value%>"></option>			</select>

    As shown above, the template uses multiple levels of if-else nesting, mixed with various When HTML and JS code are split, the structural and logical coupling is very high, the readability is relatively low, and it is not conducive to modification.

  • Multiple views of the same type have only minor differences in logic and structure. However, the source code has its own set of logic and views. The scalability is not high, resulting in a lot of code redundancy. It is not conducive to later maintenance.
  • The first step Ledi did was to extract the logic from the template and use views.SearchItemView1, views.SearchItemView20, views.SearchItemView23. These three view templates use Beyond Compare software performs text comparison and finds three template differences. The differences here include two parts: structural differences and logical differences. The tag names in the structure are all the same, but the tag attribute values ​​​​of each view are different. These attribute values ​​​​can be processed in View.

    Ledi’s initial solution was to debug each view passing in the model value, and found that the difference in view construction was in the Ctype attribute in the model value.


    So for the above three views, based on Ctype judgment, different attribute values ​​​​for different views are constructed.

    textInputAttr:function(){				var isDefault = this.model.get("IsDefault");				var searchItems = this.model.get("searchItems");				var defaultVal  = this.model.get("DefaultVal");				var cType =this.model.get("Ctype");				if(cType==1){					this.$el.find("input[type='text']").addClass("search_box_prev");					this.$el.find("input[type='text']").attr("data-rule-maxlength",300);				}else{					this.$el.find("input[type='text']").addClass("souce_name");					this.$el.find("input[type='text']").attr("data-rule-maxlength",100);					if(cType==23){						this.$el.find("input[type='text']").addClass("default_word");					}				}//针对不同ctype设置input不同属性及值				if(cType==1){					if(typeof isDefault !="undefined"&&isDefault==1)					{						this.$el.find("input[type='text']").attr("defaultValue",defaultVal);					};// 设置defaultValue属性				}								if(typeof searchItems !="undefined")				{					if(searchItems.Value!=null)					{						this.$el.find("input[type='text']").attr("value",searchItems.Value[0]);					}				}				else{					if(cType==1){						if(typeof isDefault !="undefined"&&isDefault==1){						this.$el.find("input[type='text']").attr("value",defaultVal);						}					}									}//设置value属性值			},			checkInputAttr:function(){				var searchItems = this.model.get("searchItems");				var cType =this.model.get("Ctype");				if(cType==1){					this.$el.find("input[type='checkbox']").addClass("search_box_any");				}else{					this.$el.find("input[type='checkbox']").addClass("souce_name");				}					if((typeof searchItems !="undefined")&&(searchItems.Value!=null)&&(searchItems.Value.length>0)){							_.each(searchItems.Value,function(item,value){								var valLength = (cType==1)?(searchItems.Value.length):(searchItems.Value.length-1);//判断采用何种表达式								if(valLength==value){									if(item){										this.$el.find("input[type='checkbox']").attr("value",item);										if(searchItems.Value[value]=="true")											this.$el.find("input[type='checkbox']").attr("checked","checked");									}else{										this.$el.find("input[type='checkbox']").attr("value","");									}								}							});					}else{						this.$el.find("input[type='checkbox']").attr("value","");					}			}//checkbox设置
                                                                                                                                           The structure is highly coupled and not conducive to expansion. If based on this logic, I need to add a new view that is similar to the above three types of views, then I need to improve the code again based on the code and add a Ctype judgment for the new view. This is obviously not what we want.
    The real requirement for reconstruction is: construct a general class, write the common points of each view into this class, inherit this general class in different subviews, and overwrite if there are differences General class methods to achieve personalized customization.

    With the above idea, the next step is to perform block processing based on the differences obtained through text comparison, that is, construct an atomic function and determine which atomic blocks there are. And write it into the general class together. The difference exists in the form of an empty method in the atomic class, and the general class empty function is overridden in the sub-function to achieve personalized customization.

    var SingleInputView = Talent.ItemView.extend({			onBeforeRender:function(){				this.standLabel();//标准化标签			},			onRender:function(){				this.textMaxlen(this.maxlength);				this.setCheckboxVal(this.minus);				this.SetTextInput();				this.SetCheckboxInput();			},			standLabel:function(){				var label = this.model.get("Label");				if((label.length != 7)&&(label.length>6))				{					this.model.set({"Label":label.substring(0,6)+"…"});				}			},			textAddClass:function(){			},			textMaxlen:function(length){				this.$el.find("input[type='text']").attr("data-rule-maxlength",length);			},			setTextDefaultVal:function(){			},			setTextVal:function(){				var isDefault = this.model.get("IsDefault");				var searchItems = this.model.get("searchItems");				var defaultVal  = this.model.get("DefaultVal");				if(typeof searchItems !="undefined")				{					if(searchItems.Value!=null)					{						this.$el.find("input[type='text']").val(searchItems.Value[0]);					}				}			},			checkboxAddClass:function(){			},			setCheckboxVal:function(minus){				var searchItems = this.model.get("searchItems");				if((typeof searchItems !="undefined")&&(searchItems.Value!=null)&&(searchItems.Value.length>0)){							_.each(searchItems.Value,function(item,value){								var valLength =searchItems.Value.length-minus;//判断采用何种表达式								if(valLength==value){									if(item){										this.$el.find("input[type='checkbox']").val(item);										if(searchItems.Value[value]=="true")											this.$el.find("input[type='checkbox']").attr("checked","checked");									}else{										this.$el.find("input[type='checkbox']").val("");									}								}							});					}else{						this.$el.find("input[type='checkbox']").val("");					}			},		});
    Some logical codes only have different variables with different views. Here in the parent class, construct a method with variables, and Set the attribute value in the subclass and pass it into the parent class method, such as:

    setCheckboxVal:function(minus){				var searchItems = this.model.get("searchItems");				if((typeof searchItems !="undefined")&&(searchItems.Value!=null)&&(searchItems.Value.length>0)){					_.each(searchItems.Value,function(item,value){						var valLength =searchItems.Value.length-minus;//判断采用何种表达式						if(valLength==value){							if(item){								this.$el.find("input[type='checkbox']").val(item);								if(searchItems.Value[value]=="true")									this.$el.find("input[type='checkbox']").attr("checked","checked");								}else{									this.$el.find("input[type='checkbox']").val("");								}							}					});
    Construct the post-rendering onRender in the parent class Callback function, automatically calling general class and subview methods:

    () Two methods are implemented in subclasses to load and execute different subclass functions according to customized requirements:

    onRender:function(){				this.textMaxlen(this.maxlength);				this.setCheckboxVal(this.minus);				this.SetTextInput();				this.SetCheckboxInput();			}

    🎜>General class, also uses an onBeforeRender callback method to process the data when the data has not been rendered to the template.

    SetTextInput:function(){				this.textAddClass();				this.setTextDefaultVal();				this.setTextVal();			}

    onBeforeRender:function(){				this.standLabel();//标准化标签			}

        这样处理的优势在于,逻辑更清晰,并且充分利用此回调函数时序上的优势。

        通过对以上重构分析,我们可以得出重构的大体方向:

  • 对差异代码模块化,写入通用类的空方法中。
  • 只有变量差异的代码,写入通用类中带参数方法中。
  • 最后调用方法,写在通用类中,并在子类中,构造定制化加载方法如SetTextInput方法的职能
  • Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

    HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

    The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

    The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

    Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

    HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

    What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

    The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

    HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

    How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

    Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

    What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

    HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

    How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

    TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor