// ajouter sur les champs devant être liés la variable Scope : role="databind" et variable="nom.de.la.variable" que l'on retrouvera dans scope['nom']['de']['la']['variable'] // Le databinding est bidirectionnel il suffit d'ajouter le fichier dans l'entête du html, la classe s'instentie automatiquement sur dom:loaded // // Lorsque l'on charge dynamiquement des nouveaux champs, il suffit d'appeler la fonction Refresh() de l'objet // TODO // // Mettre en place une fonctionnalité iterative "each" var Scope={}; if (!Control) { var Control={};} if (!Global) { var Global={};} document.observe("dom:loaded", function() { Global.DataBinding=new Control.ScopeController(); // $('overlay').hide(); }); Control.ScopeController=Class.create({ initialize:function() { Scope=new Proxy({},{"set":this.set.bind(this)}); this.Refresh(); }, set:function(obj,prop,value) { if (typeof value == "object") { value=new Proxy(value,{"set":this.set.bind(this)}); } obj[prop]=value; this.SetData(); }, GetRecursiveDataInObject: function(field,data) { $H(data).each(function(item){ if (typeof item.value =="object") { this.GetRecursiveDataInObject(field+"."+item.key,item.value); } else { $$('[role=databind][variable="'+field+'.'+item.key+'"]').each(function(el){ if (this.IsFormField(el)) { if (this.IsCheckbox(el)) { if (item.value=='1') { $(el).checked=true; } else { $(el).checked=false; } } else { el.value=item.value; } } else { el.update(item.value); } }.bind(this)); } }.bind(this)); }, SetData:function() { $H(Scope).each(function(item){ this.GetRecursiveDataInObject(item.key,Scope[item.key]); }.bind(this)); }, IsCheckbox:function(el) { return (el.type=="checkbox"); }, IsFormField:function(el) { var liste="INPUT,TEXTAREA,SELECT"; return (liste.indexOf(el.nodeName)>=0); }, Refresh:function() { $$('[role=databind]').each(function(el){ if (!el.retrieve('SoonLinked')) { if (this.IsFormField(el)) { el.store('SoonLinked',1); var path=el.readAttribute('variable'); var value = el.getValue(); if (value==null){value="";} p=path.split("."); this.SetRecursiveData(Scope,p,value,0); if (el.nodeName=="INPUT") { if (el.type=='checkbox') { el.observe('click',this.StoreData.bind(this)); } else { el.observe('keyup',this.StoreData.bind(this)); } } if (el.nodeName=="TEXTAREA") {el.observe('keyup',this.StoreData.bind(this));} if (el.nodeName=="SELECT") {el.observe('change',this.StoreData.bind(this));} } } }.bind(this)); }, SetRecursiveData: function(data,liste,value,pos) { if (pos>=liste.length) { data=value; return data; } else { if (!data[liste[pos]]) { data[liste[pos]]={}; } data[liste[pos]]=this.SetRecursiveData(data[liste[pos]],liste,value,pos+1); return data; } }, StoreData: function(ev) { var path=ev.findElement('[role=databind]').readAttribute('variable'); if (this.IsFormField(ev.findElement('[role=databind]'))) { if (ev.findElement('[role=databind]').nodeName=="INPUT") { if (ev.findElement('[role=databind]').type=='checkbox') { if (ev.findElement('[role=databind]').checked) { var value=1; } else { var value=0; } } else { var value=ev.findElement('[role=databind]').getValue(); } } else { var value=ev.findElement('[role=databind]').getValue(); } el=path.split("."); this.SetRecursiveData(Scope,el,value,0); } } }); function jsonrequest(uri,method,data,callback,failedcallback) { Object.keys(data).each(function(key){ if (typeof data[key] === "object") { data[key]=Object.toJSON(data[key]); } }); new Ajax.Request(uri, { method: method, parameters: data, onSuccess: function(result) { if (result.responseJSON.header.response=="error") { $('errorboxmessage').update(); result.responseJSON.header.errors.each(function(item){ $('errorboxmessage').insert(item+"
"); }); $('overlay').show(); } else { if (result.responseJSON.header.status) { if (result.responseJSON.header.status=="notlogged") { location.reload();} } callback(result); } }, onFailure: function(result) { if (typeof failedcallback !='undefined') { failedcallback(result);} } }); } function executeJS(js) { eval(js).bind(this); }