雙向資料繫結

來源:互聯網
上載者:User

標籤:trigger   雙向   val   log   object_id   put   return   textarea   事件   

<textarea type="number"  data-bind-123="name" /></textarea><br><br><textarea type="number"  data-bind-456="name1" /></textarea><script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script><script>// object_id 對象idfunction DataBinder(object_id){  //使用jQuery對象做一個簡單訂閱  var pubSub=jQuery({});  var data_attr="bind-"+object_id,    message=object_id+":change";  //監聽change事件  jQuery(document).on("change","[data-"+data_attr+"]",function(eve){    var $input=jQuery(this); //觸發事件    pubSub.trigger(message,[$input.data(data_attr),$input.val()]);  });  //  pubSub.on(message,function(evt,prop_name,new_val){  //    jQuery("[data-"+data_attr+"="+prop_name+"]").each(function(){      var $bound=jQuery(this);      if($bound.is("")){        $bound.html(new_val);      }else{        $bound.html(new_val);      }    });  });  return pubSub;}//function User(uid){  var binder=new DataBinder(uid),    user={      attributes:{},      set:function(attr_name,val){        this.attributes[attr_name]=val;        binder.trigger(uid+":change",[attr_name,val,this]);      },      get:function(attr_name){        return this.attributes[attr_name];      },      _binder:binder    };  // Subscribe to PubSub  binder.on(uid+":change",function(evt,attr_name,new_val,initiator){    if(initiator!==user){      user.set(attr_name,new_val);    }  });  return user;}//var user=new User(123);user.set("name","Wolfgang");//var user1=new User(456);user1.set("name1","Wolfgang789");//console.log(user.get("name"));console.log(user1.get("name1"));</script>

通過change事件來實現,1.試圖端change觸發修改服務端dade, 服務端dade修改觸發change修改頁面。

雙向資料繫結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.