model-min.js revision d74a6f9bc3118b707ff2d7d6bc5505dd66faddc6
YUI.add("model",function(a){var i=YUI.namespace("Env.Model"),f=a.Lang,j=a.Array,h=a.Object,g="change",d="error",e="load",c="save";function b(){b.superclass.constructor.apply(this,arguments);}a.Model=a.extend(b,a.Base,{idAttribute:"id",initializer:function(k){this.changed={};this.lastChange={};this.lists=[];},destroy:function(l,n){var k=this;if(typeof l==="function"){n=l;l={};}function m(o){if(!o){j.each(k.lists.concat(),function(p){p.remove(k,l);});b.superclass.destroy.call(k);}n&&n.apply(null,arguments);}if(l&&l["delete"]){this.sync("delete",l,m);}else{m();}return this;},generateClientId:function(){i.lastId||(i.lastId=0);return this.constructor.NAME+"_"+(i.lastId+=1);},getAsHTML:function(k){var l=this.get(k);return a.Escape.html(f.isValue(l)?String(l):"");},getAsURL:function(k){var l=this.get(k);return encodeURIComponent(f.isValue(l)?String(l):"");},isModified:function(){return this.isNew()||!h.isEmpty(this.changed);},isNew:function(){return !f.isValue(this.get("id"));},load:function(l,m){var k=this;if(typeof l==="function"){m=l;l={};}l||(l={});k.sync("read",l,function(q,o){var p={options:l,response:o},n;if(q){p.error=q;p.src="load";k.fire(d,p);}else{if(!k._loadEvent){k._loadEvent=k.publish(e,{preventable:false});}n=p.parsed=k.parse(o);k.setAttrs(n,l);k.changed={};k.fire(e,p);}m&&m.apply(null,arguments);});return k;},parse:function(k){if(typeof k==="string"){try{return a.JSON.parse(k);}catch(l){this.fire(d,{error:l,response:k,src:"parse"});return null;}}return k;},save:function(m,n){var l=this,k=l._validate(l.toJSON());if(typeof m==="function"){n=m;m={};}if(!k.valid){n&&n.call(null,k.error);return l;}m||(m={});l.sync(l.isNew()?"create":"update",m,function(r,p){var q={options:m,response:p},o;if(r){q.error=r;q.src="save";l.fire(d,q);}else{if(!l._loadEvent){l._loadEvent=l.publish(e,{preventable:false});}if(p){o=q.parsed=l.parse(p);l.setAttrs(o,m);}l.changed={};l.fire(c,q);}n&&n.apply(null,arguments);});return l;},set:function(m,n,l){var k={};k[m]=n;return this.setAttrs(k,l);},setAttrs:function(k,l){var o=this.idAttribute,r,p,m,n,q;l||(l={});q=l._transaction={};if(o!=="id"){k=a.merge(k);if(h.owns(k,o)){k.id=k[o];}else{if(h.owns(k,"id")){k[o]=k.id;}}}for(m in k){if(h.owns(k,m)){this._setAttr(m,k[m],l);}}if(!h.isEmpty(q)){r=this.changed;n=this.lastChange={};for(m in q){if(h.owns(q,m)){p=q[m];r[m]=p.newVal;n[m]={newVal:p.newVal,prevVal:p.prevVal,src:p.src||null};}}if(!l.silent){if(!this._changeEvent){this._changeEvent=this.publish(g,{preventable:false});}this.fire(g,{changed:n});}}return this;},sync:function(){var k=j(arguments,0,true).pop();if(typeof k==="function"){k();}},toJSON:function(){var k=this.getAttrs();delete k.clientId;delete k.destroyed;delete k.initialized;if(this.idAttribute!=="id"){delete k.id;}return k;},undo:function(p,l){var o=this.lastChange,n=this.idAttribute,k={},m;p||(p=h.keys(o));j.each(p,function(q){if(h.owns(o,q)){q=q===n?"id":q;m=true;k[q]=o[q].prevVal;}});return m?this.setAttrs(k,l):this;},validate:function(){},addAttr:function(l,k,n){var o=this.idAttribute,m,p;if(o&&l===o){m=this._isLazyAttr("id")||this._getAttrCfg("id");p=k.value===k.defaultValue?null:k.value;if(!f.isValue(p)){p=m.value===m.defaultValue?null:m.value;if(!f.isValue(p)){p=f.isValue(k.defaultValue)?k.defaultValue:m.defaultValue;}}k.value=p;if(m.value!==p){m.value=p;if(this._isLazyAttr("id")){this._state.add("id","lazy",m);}else{this._state.add("id","value",p);}}}return b.superclass.addAttr.apply(this,arguments);},_validate:function(k){var l=this.validate(k);if(f.isValue(l)){this.fire(d,{attributes:k,error:l,src:"validate"});return{valid:false,error:l};}return{valid:true};},_defAttrChangeFn:function(l){var k=l.attrName;if(!this._setAttrVal(k,l.subAttrName,l.prevVal,l.newVal)){l.stopImmediatePropagation();}else{l.newVal=this.get(k);if(l._transaction){l._transaction[k]=l;}}}},{NAME:"model",ATTRS:{clientId:{valueFn:"generateClientId",readOnly:true},id:{value:null}}});},"@VERSION@",{requires:["base-build","escape","json-parse"]});