php 插件化开发,JavaScript插件化开发教程(六)_javascript技巧

一,开篇分析

今天这篇文章我们说点什么那?嘿嘿嘿。我们接着上篇文章对不足的地方进行重构,以深入浅出的方式来逐步分析,让大家有一个循序渐进提高的过程。废话少说,进入正题。让我们先来回顾一下之前的

Js部分的代码,如下:

function ItemSelector(elem,opts){

this.elem = elem ;

this.opts = opts ;

} ;

var ISProto = ItemSelector.prototype ;

ISProto.getElem = function(){

return this.elem ;

} ;

ISProto.getOpts = function(){

return this.opts ;

} ;

/* data manip*/

ISProto._setCurrent = function(current){

this.getOpts()["current"] = current ;

} ;

ISProto.getCurrentValue = function(current){

return this.getOpts()["current"] ;

} ;

/* data manip*/

ISProto.init = function(){

var that = this ;

this.getOpts()["current"] = null ; // 数据游标

this._setItemValue(this.getOpts()["currentText"]) ;

var itemsElem = that.getElem().find(".content .items") ;

this.getElem().find(".title div").on("click",function(){

itemsElem.toggle() ;

}) ;

this.getElem().find(".title span").on("click",function(){

itemsElem.toggle() ;

}) ;

$.each(this.getOpts()["items"],function(i,item){

item["id"] = (new Date().getTime()).toString() ;

that._render(item) ;

}) ;

} ;

ISProto._setItemValue = function(value){

this.getElem().find(".title div").text(value)

} ;

ISProto._render = function(item){

var that = this ;

var itemElem = $("

")

.text(item["text"])

.attr("id",item["id"]) ;

if("0" == item["disabled"]){

itemElem.on("click",function(){

var onChange = that.getOpts()["change"] ;

that.getElem().find(".content .items").hide() ;

that._setItemValue(item["text"]) ;

that._setCurrent(item) ;

onChange && onChange(item) ;

})

.mouseover(function(){

$(this).addClass("item-hover") ;

})

.mouseout(function(){

$(this).removeClass("item-hover") ;

}) ;

}

else{

itemElem.css("color","#ccc").on("click",function(){

that.getElem().find(".content .items").hide() ;

that._setItemValue(item["text"]) ;

}) ;

}

itemElem.appendTo(this.getElem().find(".content .items")) ;

} ;

效果如下图所示:

d04570f48953d18c53484391b50cb5e1.png

a)------非可操作状态

58544d93524da595e4b36fc86b66e41e.png

b)------可操作状态

aa7c481d21801e272349ffb1f8b11d8c.png

(二),打开思路,进行重构

大家从代码不难看出,已经通过“Js”中的语法特性,以面向对象的方式进行了有效的组织,比松散的过程化形式的组织方式好多了,但是仍然会发现有很多不足的地方。

(1),里面重复代码太多

(2),职责划分不清晰

(3),流程梳理不健全

我们基于以上几点进行有效的重构,我们首先要梳理一下这个组件的需求,功能点如下:

(1),初始化配置组件

$(function(){

var itemSelector = new ItemSelector($("#item-selector"),{

currentText : "Please Choose Item" ,

items : [

{

text : "JavaScript" ,

value : "js" ,

disabled : "1"

} ,

{

text : "Css" ,

value : "css" ,

disabled : "0"

} ,

{

text : "Html" ,

value : "html" ,

disabled : "0"

}

] ,

}) ;

itemSelector.init() ;

}) ;

这块代码很清晰,不需要做任何修改,但是大家可以基于以上配置扩展功能,比如增加配置项“mode”支持多种选项方式。如:“checkbox勾选模式”。

接下来是要完成初始化逻辑,如下:

ISProto.init = function(){

var that = this ;

this.getOpts()["current"] = null ; // 数据游标

this._setItemValue(this.getOpts()["currentText"]) ;

var itemsElem = that.getElem().find(".content .items") ;

this.getElem().find(".title div").on("click",function(){

itemsElem.toggle() ;

}) ;

this.getElem().find(".title span").on("click",function(){

itemsElem.toggle() ;

}) ;

$.each(this.getOpts()["items"],function(i,item){

item["id"] = (new Date().getTime()).toString() ;

that._render(item) ;

}) ;

} ;

这段代码问题很多,职责不明确,初始化逻辑包含了功能点的细节实现。

再继续看渲染部分代码:

ISProto._render = function(item){

var that = this ;

var itemElem = $("

")

.text(item["text"])

.attr("id",item["id"]) ;

if("0" == item["disabled"]){

itemElem.on("click",function(){

var onChange = that.getOpts()["change"] ;

that.getElem().find(".content .items").hide() ;

that._setItemValue(item["text"]) ;

that._setCurrent(item) ;

onChange && onChange(item) ;

})

.mouseover(function(){

$(this).addClass("item-hover") ;

})

.mouseout(function(){

$(this).removeClass("item-hover") ;

}) ;

}

else{

itemElem.css("color","#ccc").on("click",function(){

that.getElem().find(".content .items").hide() ;

that._setItemValue(item["text"]) ;

}) ;

}

itemElem.appendTo(this.getElem().find(".content .items")) ;

} ;

问题很明显,发现了重复性的操作,应该进行合理的抽象,已达到复用的目的。

整个组建的流程包括初始化,渲染(事件绑定),还有就是相关的数据操作方法以及dom操作的辅助方法。

综上所述,经过简单的梳理后,我们应该建立起功能的操作目的以及流程主线的任务分配,各负其责。

所以我们重构的目的很明确了,对!就是进行功能点的抽象,友好的职责划分,那么我们如何实现那?

第一步,建立流程功能方法:(方法接口)

ISProto.init = function(){

// put you code here !

} ;

ISProto._render = function(){

// put you code here !

} ;

第二部,建立抽象后的方法接口:

ISProto._fnItemSelectorDelegateHandler = function(){

// put you code here !

} ;

ISProto._fnTriggerHandler = function(){

// put you code here !

} ;

ISProto._addOrRemoveClass = function(){

// put you code here !

} ;

第三步,建立数据操作接口:

ISProto._setCurrent = function(){

// put you code here !

} ;

ISProto._getCurrent = function(){

// put you code here !

} ;

还有一些参照下面的完整源码,这里只是说的思路。

(三),完整代码以供学习,本代码已经过测试

function ItemSelector(elem,opts){

this.elem = elem ;

this.opts = opts ;

this.current = -1 ; // 数据游标

} ;

var ISProto = ItemSelector.prototype ;

/* getter api*/

ISProto.getElem = function(){

return this.elem ;

} ;

ISProto.getOpts = function(){

return this.opts ;

} ;

ISProto._getCurrent = function(){

return this.current ;

} ;

/* getter api*/

/* data manip*/

ISProto._setCurrent = function(current){

this.current = current ;

} ;

ISProto._setItemText = function(text){

this.getElem().find(".title div").text(text) ;

} ;

/* data manip*/

/* update on 2015 1/31 23:38 */

ISProto._fnTriggerHandler = function(index,text,value){

if(this._isDisabled(value)){

index = -1 ;

text = this.getOpts()["currentText"] ;

}

this._setItemText(text) ;

this._setCurrent(index) ;

this.getElem().find(".content .items").hide() ;

} ;

ISProto._addOrRemoveClass = function(elem,className,addIs){

if(addIs){

elem.addClass(className) ;

}

else{

elem.removeClass(className) ;

}

} ;

ISProto._fnItemSelectorDelegateHandler = function(){

var that = this ;

this.getElem().on("click","[data-toggle]",function(){

that.getElem().find(".content .items").toggle() ;

}) ;

} ;

ISProto._isDisabled = function(value){

return ("1" == value) ? true : false ;

} ;

/* update on 2015 1/31 23:38 */

ISProto.init = function(){

var that = this ;

this._fnItemSelectorDelegateHandler() ;

$.each(this.getOpts()["items"],function(i,item){

item["index"] = i ;

that._render(item) ;

}) ;

this._fnTriggerHandler(this._getCurrent(),this.getOpts()["currentText"],"1") ;

} ;

ISProto._render = function(item){

var that = this ;

var itemElem = $("

").text(item["text"]).attr("id",item["index"]) ;

var activeClass = ("0" == item["disabled"]) ? "item-hover" : "item-disabled-hover" ;

itemElem.on("click",function(){

that._fnTriggerHandler(item["index"],item["text"],item["disabled"]) ;

})

.mouseover(function(){

that._addOrRemoveClass($(this),activeClass,true) ;

})

.mouseout(function(){

that._addOrRemoveClass($(this),activeClass,false) ;

}) ;

itemElem.appendTo(this.getElem().find(".content .items")) ;

} ;

(四),最后总结

(1),面向对象的思考方式合理分析功能需求。

(2),以类的方式来组织我们的插件逻辑。

(3),不断重构上面的实例,如何进行合理的重构那?不要设计过度,要游刃有余,推荐的方式是过程化设计与面向对象思想设计相结合。

(4),下篇文章中会扩展相关功能,比如“mode”这个属性,为"1"时支持checkbox多选模式,现在只是默认下拉模式。

看我本文,是不是要比上一篇代码优秀了很多呢,小伙伴们自己做项目也应该多想多做,尽量使自己的代码更加的合理。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值