js插件對象原型

基于requirejs的js控件基類
github:https://github.com/milolu/blog/tree/master/jq-widget

/**
 * Created by Silence on 2014/10/27.
 * Describe
 */

define(function(){
    function Widget(){
        this.boundingBox = null;    //屬性,最外層容器
    }
    Widget.prototype = {
        //綁定事件
        on : function(type, handler){
            if(typeof this.handlers[type] == "undefined"){
                this.handlers[type] = [];
            }
            this.handlers[type].push(handler);
            return this
        },
        //調(diào)用綁定事件
        fire : function(type, data){
            if(this.handlers[type] instanceof Array){
                var handlers = this.handlers[type];
                for(var i = 0; i < handlers.length; i++){
                    handlers[i](data);
                }
            }
        },
        //渲染組件
        render : function(container){
            this.renderUI();
            this.handlers = {};
            this.bindUI();
            this.syncUI();
            //$(container || document.body).append(this.boundingBox);
        },
        //銷毀組件
        destroy : function(){
            this.destructor();
            this.boundingBox.off();
            this.boundingBox.remove();
        },
        //獲取元素本身
        self : function(){
            return this.boundingBox;
        },
        //獲取寬度
        width : function(){
            return this.boundingBox.width();
        },
        //獲取高度
        height : function(){
            return this.boundingBox.height();
        },
        //添加dom節(jié)點
        renderUI : function(){},
        //監(jiān)聽事件
        bindUI : function(){},
        //初始化組件屬性
        syncUI : function(){},
        //銷毀前的處理函數(shù)
        destructor : function(){}

    };
    return {
        Widget : Widget
    }
});

選擇控件實現(xiàn)樣例

/**
 * Created by Silence on 2014/10/30.
 * Describe 選擇控件
 * 參數(shù)
 *  parent :父節(jié)點("#parent")
 *  width:選擇框的大行刑啊(除標(biāo)題外的寬度)
 *  css:樣式
 *  skinClassName : 皮膚類名
 *  values :可選擇項
 *  onSelect(index, data) : 選擇事件
 *  defaultSel : 默認(rèn)選中項
 *  title:選擇器的標(biāo)題
 *
 * API:
 *  setSelectIndex(index, isCallBack):  設(shè)置選中項(選中序號朋凉,是否進(jìn)行選中事件羽圃,默認(rèn)不調(diào)用)
 *  getSelectValue: 獲取當(dāng)前選中序號及內(nèi)容
 */

define(["widget"], function(widget){
    function Select(){
        this.cfg = {
            parent : null,
            width : null,
            title : null,
            css : null,
            skinClassName : null,
            values : ["sel1", "sel2", "sel3", "sel4"],
            key : null,
            onSelect : null,
            defaultSel : 0,
            firstCallBack : true,
            enable : true
        };
    }
    Select.prototype = $.extend({}, new widget.Widget(), {
        create : function(cfg){
            $.extend(this.cfg, cfg);
            this._select = this.cfg.defaultSel;
            this.render();
            return this;
        },
        renderUI : function(){
            var htm = "<div class='w-select-box'>{{if title}}<span class='w-select-title'>${title}</span>{{/if}}<div class='w-select ${skinClassName}'><span class='w-select-value'></span>" +
                "<div class='w-select-btn'></div>" +
                "<div class='w-select-values'><ul></ul></div>" +
                "</div></div>";

            this.boundingBox = $($.tmpl(htm, this.cfg));
            var $ul = this.boundingBox.find("ul");
            for(var i=0; i<this.cfg.values.length; i++)
            {
                if(this.cfg.key != null)
                    $ul.append("<li>"+ this.cfg.values[i][this.cfg.key] +"</li>");
                else
                    $ul.append("<li>"+ this.cfg.values[i] +"</li>");
            }

            this.btn = this.boundingBox.find(".w-select-btn");
            this.valuesBar = this.boundingBox.find(".w-select-values");
            this.lis = this.boundingBox.find("li");
            if(this.cfg.parent != null)
                this.cfg.parent.append(this.boundingBox);
        },
        bindUI : function(){
            var that = this;
            this.on("show", function(){
                if(that.cfg.enable == false)
                    return;
                $(".w-select-btn").removeClass("w-select-btn-showing");
                $(".w-select-values").slideUp(100);
                that.btn.addClass("w-select-btn-showing");
                that.valuesBar.slideDown(400);
            });
            this.on("close", function(){
                that.btn.removeClass("w-select-btn-showing");
                that.valuesBar.slideUp(400);
            });
            this.on("selected", function(data){
                if(that.cfg.onSelect != null){
                    that.cfg.onSelect(data.sel, data.value);
                }
                //that.fire("close");
            });

            this.boundingBox.click(function(event){
                if(that.btn.hasClass("w-select-btn-showing"))
                {
                    that.fire("close");
                }
                else
                {
                    that.fire("show");
                }
                event.cancelBubble = true;
                event.stopPropagation();
            });

            this.lis.click(function(){
                var i = that.lis.index(this);
                that.setSelectIndex(i, true);
            });
        },
        syncUI : function(){
            //初始化設(shè)置選中項
            this.setSelectIndex(this.cfg.defaultSel, this.cfg.firstCallBack);
            this.setEnable(this.cfg.enable);
            if(this.cfg.css != null)
                this.boundingBox.css(this.cfg.css);
            if(this.cfg.width != null)
                this.boundingBox.find(".w-select").css("width", this.cfg.width);
        },
        destructor : function(){
        },
        //設(shè)置選中項
        setSelectIndex : function(index, isCallBack){
            isCallBack = isCallBack || false;

            this._select = index;
            var value = this.cfg.values[index];
            if(this.cfg.key != null)
                value = value[this.cfg.key];
            this.boundingBox.find(".w-select-value").empty().append( value );
            if(isCallBack)
            {
                this.fire("selected", { sel:index, value:this.cfg.values[index] });
            }
        },
        setSelectIndexByValue : function(value, isCallBack){
            for(var i in this.cfg.values){
                var thisValue = this.cfg.values[i];
                if(this.cfg.key != null)
                    thisValue = thisValue[this.cfg.key];
                if(value == thisValue)
                {
                    this.setSelectIndex(i, isCallBack);
                    break;
                }
            }
        },
        setEnable : function(type){
            this.cfg.enable = type;
            if(type == true){
                this.boundingBox.removeClass("w-select-box-unable");
            }
            else{
                this.boundingBox.addClass("w-select-box-unable");
            }
        },
        setSelectValues : function(_values, _defaultSel, _iscallback){
            var defaultSel = _defaultSel || 0;
            var iscallback = _iscallback || true;
            var $ul = this.boundingBox.find("ul");
            $ul.empty();
            this.cfg.values = _values;
            for(var i=0; i<this.cfg.values.length; i++)
            {
                if(this.cfg.key != null)
                    $ul.append("<li>"+ this.cfg.values[i][this.cfg.key] +"</li>");
                else
                    $ul.append("<li>"+ this.cfg.values[i] +"</li>");
            }
            this.setSelectIndex(defaultSel, iscallback);
        },
        //獲取當(dāng)前選中的內(nèi)容
        getSelectValue : function(){
            var value = this.cfg.values[this._select];
            if(this.cfg.key != null)
                value = value[this.cfg.key];
            return {
                sel : this._select,
                value : value
            }
        }
    });
    if(typeof Select._init == 'undefined')
    {
        Select._init = true;
        $('body').on('click', function(){
            $(".w-select-btn").removeClass("w-select-btn-showing");
            $(".w-select-values").slideUp(100);
        });
    }
    return {
        Select : Select
    }
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末设联,一起剝皮案震驚了整個濱河市恕曲,隨后出現(xiàn)的幾起案子切心,更是在濱河造成了極大的恐慌飒筑,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绽昏,死亡現(xiàn)場離奇詭異协屡,居然都是意外死亡,警方通過查閱死者的電腦和手機全谤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門肤晓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事补憾÷眩” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵盈匾,是天一觀的道長卷胯。 經(jīng)常有香客問我,道長威酒,這世上最難降的妖魔是什么窑睁? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮葵孤,結(jié)果婚禮上担钮,老公的妹妹穿的比我還像新娘。我一直安慰自己尤仍,他們只是感情好箫津,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宰啦,像睡著了一般苏遥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赡模,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天田炭,我揣著相機與錄音,去河邊找鬼漓柑。 笑死教硫,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的辆布。 我是一名探鬼主播瞬矩,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼锋玲!你這毒婦竟也來了景用?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤惭蹂,失蹤者是張志新(化名)和其女友劉穎伞插,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剿干,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蜂怎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了置尔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡氢伟,死狀恐怖榜轿,靈堂內(nèi)的尸體忽然破棺而出幽歼,到底是詐尸還是另有隱情,我是刑警寧澤谬盐,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布甸私,位于F島的核電站,受9級特大地震影響飞傀,放射性物質(zhì)發(fā)生泄漏皇型。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一砸烦、第九天 我趴在偏房一處隱蔽的房頂上張望弃鸦。 院中可真熱鬧,春花似錦幢痘、人聲如沸唬格。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽购岗。三九已至,卻和暖如春门粪,著一層夾襖步出監(jiān)牢的瞬間喊积,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工玄妈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留注服,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓措近,卻偏偏與公主長得像溶弟,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子瞭郑,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容