自己寫的javascript綁定模型 基於jquery,javascriptjquery

來源:互聯網
上載者:User

自己寫的javascript綁定模型 基於jquery,javascriptjquery

//綁定模型$.bind = {};//綁定基礎$.bind.base = function (get, set) {    return function (o) {        if (typeof (o) != 'undefined') {            set(o);        };        return get();    };};


//綁定文本$.bind.text = function (element,func) {    var $el = element;    return $.bind.base(function () {        return $el.text();    }, function (o) {        $el.text(o);        if (typeof (func) == 'function') {            func($el, o);        };    });};//綁定值$.bind.val = function (element, func) {    var $el = element;    return $.bind.base(function () {        return $el.val();    }, function (o) {        $el.val(o);        if (typeof (func) == 'function') {            func($el, o);        };    });};//綁定html$.bind.html = function (element, func) {    var $el = element;    return $.bind.base(function () {        return $el.html();    }, function (o) {        $el.html(o);        if (typeof (func) == 'function') {            func($el, o);        };    });};


//綁定bool$.bind.bool = function (element, func) {    var $el = element;    var obj = true;    return $.bind.base(function () {        return obj;    }, function (o) {        obj = o;        if (typeof (func) == 'function') {            func($el, o);        };    });};//根據bool 增加 移除css$.bind.cssClass = function (element, css, check, func) {    var $el = element;    var obj = true;    return $.bind.base(function () {        return obj;    }, function (o) {        if (check && obj == o) {            return;        }        obj = o;        if (o) {            $el.addClass(css);        } else {            $el.removeClass(css);        }        if (typeof (func) == 'function') {            func($el, o);        };    });};//根據bool 顯示 隱藏$.bind.visibility = function (element, check, func) {    var $el = element;    var obj = true;    return $.bind.base(function () {        return obj;    }, function (o) {        if (check && obj == o) {            return;        }        obj = o;        if (o) {            $el.show();        } else {            $el.hide();        }        if (typeof (func) == 'function') {            func($el, o);        };    });};

//綁定隱藏值$.bind.hide = function (func) {    var obj = true;    return $.bind.base(function () {        return obj;    }, function (o) {        obj = o;        if (typeof (func) == 'function') {            func(o);        };    });};//綁定數組$.bind.array = function (element, func) {    var array = [];    var $el = element;    return $.bind.base(function () {        return array;    }, function (o) {        array = o;        $el.empty();        if (typeof (func) == 'function') {            $.each(o, function (i, v) {                func($el, i, v);            });        };    });};//綁定對象$.bind.obj = function (o, func) {    var list = [];    var obj = {};    for (var item in o) {        var temp = {};        temp.o = item;        var temp_obj = o[temp.o];        switch (temp_obj.t) {            case 'array':                temp.obj = $.bind.array(temp_obj.el, temp_obj.f);                break;            case 'text':                temp.obj = $.bind.text(temp_obj.el, temp_obj.f);                break;            case 'val':                temp.obj = $.bind.val(temp_obj.el, temp_obj.f);                break;            case 'html':                temp.obj = $.bind.html(temp_obj.el, temp_obj.f);                break;            case 'bool':                temp.obj = $.bind.bool(temp_obj.el, temp_obj.f);                break;            case 'hide':                temp.obj = $.bind.hide(temp_obj.f);                break;            case 'cssClass':                temp.obj = $.bind.cssClass(temp_obj.el, temp_obj.css, temp_obj.ck, temp_obj.f);                break;            case 'visi':                temp.obj = $.bind.visibility(temp_obj.el, temp_obj.ck, temp_obj.f);                break;            case 'visibility':                temp.obj = $.bind.visibility(temp_obj.el, temp_obj.ck, temp_obj.f);                break;            default:        }        list.push(temp);    };    return function (o) {        if (typeof (o) != 'undefined') {            obj = o;            $.each(list, function (i, v) {                v.obj(o[v.o]);            });            if (typeof (func) == 'function') {                func(o);            };        };        return obj;    };};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.