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