Write your own JavaScript binding model based on jquery

Source: Internet
Author: User

Binding model $.bind = {};//binding base $.bind.base = function (get, set) {    return function (o) {        if (typeof (o)! = ' undefined ') {            set (o);        };        return get ();    };


Bound Text $.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);        };    });};/ /bound Value $.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);        };    });};/ /Bind 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);};};};    


Bind 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);    }; });};/    /Add Remove Css$.bind.cssclass = function (element, CSS, check, func) {var $el = element according to BOOL;    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);    }; });};/    /based on bool Show hidden $.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 hidden Value $.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 $.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);        });    }; });};/    /bound Object $.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; };};



Write your own JavaScript binding model based on jquery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.