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>
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设置
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();//标准化标签 }
通过对以上重构分析,我们可以得出重构的大体方向: