The self-written javascript binding model is based on jquery

Source: Internet
Author: User

The self-written javascript binding model is based on jquery

// Bind the model $. bind ={}; // bind the base $. bind. base = function (get, set) {return function (o) {if (typeof (o )! = 'Undefined') {set (o) ;}; return get ();};};


// Bind the 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) ;}) ;}; // bind the 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 = function (element, func) {var $ el = element; return $. bind. base (function () {return cancel.html () ;}, function (o) {cancel.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 and remove css$.bind.css Class = function (element, css, check, func) {var $ el = element; var obj = true according to bool; 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 );};});}; // hide $. 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 the 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 an 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, funct Ion (I, v) {func ($ el, I, v) ;};}) ;}; // bind 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, te Mp_obj.f); break; case 'html ': temp. obj = pai.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 = pai.bind.css Class (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); bre Ak; 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 ;};};



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.