


Applicant 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:
<select data-name="<%=Name%>" data-obj="<%=controlData.Object%>" class="souce_name search_view width130"> <option value="">不限</option> <%if(typeof searchItems !="undefined"){%> <%if(searchItems.Value!=null){%> <%_.each(dataSource, function(item){%> <%if(searchItems.Value.length>0){%> <%_.each(searchItems.Value, function(item1){%> <%if(item1!=item.Value){%> <option value="<%=item.Value%>"><%=item.Text%></option> <%}else{%> <option value="<%=item.Value%>" selected="selected"><%=item.Text%></option> <%}%> <%})%> <%}else{%> <option value="<%=item.Value%>"><%=item.Text%></option> <%}%> <%})%> <%}else{%> <%_.each(dataSource, function(item){%> <option value="<%=item.Value%>" title="<%=item.Text%>"><%=item.Text%></option> <%})%> <%}%> <%}else{%> <%_.each(dataSource, function(item){%> <option value="<%=item.Value%>"><%=item.Text%></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.
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 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(""); } }, });
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(""); } } });
() 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();//标准化标签 }
这样处理的优势在于,逻辑更清晰,并且充分利用此回调函数时序上的优势。
通过对以上重构分析,我们可以得出重构的大体方向:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.
