JsonRest.js 5.73 KB
//>>built
define("dojox/rpc/JsonRest",["dojo","dojox","dojox/json/ref","dojox/rpc/Rest"],function(p,g){function u(a,c,b,d){(c=c.ioArgs&&c.ioArgs.xhr&&c.ioArgs.xhr.getResponseHeader("Last-Modified"))&&q._timeStamps&&(q._timeStamps[d]=c);var e=a._schema&&a._schema.hrefProperty;e&&(g.json.ref.refAttribute=e);b=b&&g.json.ref.resolveJson(b,{defaultId:d,index:q._index,timeStamps:c&&q._timeStamps,time:c,idPrefix:a._store.allowNoTrailingSlash?a.servicePath+"/":a.servicePath.replace(/[^\/]*$/,""),idAttribute:l.getIdAttribute(a),
schemas:l.schemas,loader:l._loader,idAsRef:a.idAsRef,assignAbsoluteIds:!0});g.json.ref.refAttribute="$ref";return b}var m=[],q=g.rpc.Rest,l;l=g.rpc.JsonRest={serviceClass:g.rpc.Rest,conflictDateHeader:"If-Unmodified-Since",commit:function(a){a=a||{};for(var c=[],b={},d=[],e=0;e<m.length;e++){var h=m[e],f=h.object,g=h.old;if((!a.service||!f&&!g||!(f||g).__id.indexOf(a.service.servicePath))&&h.save){delete f.__isDirty;if(f)if(g){var k;if(k=f.__id.match(/(.*)#.*/))f=q._index[k[1]];if(!(f.__id in b)){b[f.__id]=
f;if(a.incrementalUpdates&&!k)var n=("function"==typeof a.incrementalUpdates?a.incrementalUpdates:function(){n={};for(var a in f)if(f.hasOwnProperty(a))f[a]!==g[a]&&(n[a]=f[a]);else if(g.hasOwnProperty(a))return null;return n})(f,g);n?c.push({method:"post",target:f,content:n}):c.push({method:"put",target:f,content:f})}}else k=l.getServiceAndId(f.__id).service,l.getIdAttribute(k)in f&&!a.alwaysPostNewItems?c.push({method:"put",target:f,content:f}):c.push({method:"post",target:{__id:k.servicePath},
content:f});else g&&c.push({method:"delete",target:g});d.push(h);m.splice(e--,1)}}p.connect(a,"onError",function(){if(!1!==a.revertOnError){var b=m;m=d;l.revert();m=b}else p.forEach(d,function(a){l.changing(a.object,!a.object)})});l.sendToServer(c,a);return c},sendToServer:function(a,c){var b=p.xhr,d=a.length,e,h,f,m=this.conflictDateHeader;p.xhr=function(c,d){d.headers=d.headers||{};d.headers.Transaction=a.length-1==e?"commit":"open";m&&f&&(d.headers[m]=f);h&&(d.headers["Content-ID"]="\x3c"+h+"\x3e");
return b.apply(p,arguments)};for(e=0;e<a.length;e++){var k=a[e];g.rpc.JsonRest._contentId=k.content&&k.content.__id;var n="post"==k.method;(f="put"==k.method&&q._timeStamps[k.content.__id])&&(q._timeStamps[k.content.__id]=new Date+"");h=n&&g.rpc.JsonRest._contentId;var r=l.getServiceAndId(k.target.__id),n=r.service,r=k.deferred=n[k.method](r.id.replace(/#/,""),g.json.ref.toJson(k.content,!1,n.servicePath,!0));(function(b,e,f){e.addCallback(function(g){try{var h=e.ioArgs.xhr&&e.ioArgs.xhr.getResponseHeader("Location");
if(h){var k=h.match(/(^\w+:\/\/)/)&&h.indexOf(f.servicePath),h=0<k?h.substring(k):(f.servicePath+h).replace(/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/,"$2$3");b.__id=h;q._index[h]=b}g=u(f,e,g,b&&b.__id)}catch(v){}--d||c.onComplete&&c.onComplete.call(c.scope,a);return g})})(k.content,r,n);r.addErrback(function(a){d=-1;c.onError.call(c.scope,a)})}p.xhr=b},getDirtyObjects:function(){return m},revert:function(a){for(var c=m.length;0<c;){c--;var b=m[c],d=b.object,b=b.old,e=g.data._getStoreForItem(d||
b);if(!a||!d&&!b||!(d||b).__id.indexOf(a.servicePath)){if(d&&b){for(var h in b)if(b.hasOwnProperty(h)&&d[h]!==b[h]){if(e)e.onSet(d,h,d[h],b[h]);d[h]=b[h]}for(h in d)if(!b.hasOwnProperty(h)){if(e)e.onSet(d,h,d[h]);delete d[h]}}else if(!b){if(e)e.onDelete(d)}else if(e)e.onNew(b);delete (d||b).__isDirty;m.splice(c,1)}}},changing:function(a,c){if(a.__id){a.__isDirty=!0;for(var b=0;b<m.length;b++){var d=m[b];if(a==d.object){c&&(d.object=!1,this._saveNotNeeded||(d.save=!0));return}}d=a instanceof Array?
[]:{};for(b in a)a.hasOwnProperty(b)&&(d[b]=a[b]);m.push({object:!c&&a,old:d,save:!this._saveNotNeeded})}},deleteObject:function(a){this.changing(a,!0)},getConstructor:function(a,c){if("string"==typeof a){var b=a;a=new g.rpc.Rest(a,!0);this.registerService(a,b,c)}if(a._constructor)return a._constructor;a._constructor=function(b){function c(a){if(a){c(a["extends"]);t=a.properties;for(var b in t){var e=t[b];e&&"object"==typeof e&&"default"in e&&(d[b]=e["default"])}}a&&a.prototype&&a.prototype.initialize&&
(k=!0,a.prototype.initialize.apply(d,f))}var d=this,f=arguments,t,k;c(a._schema);!k&&b&&"object"==typeof b&&p.mixin(d,b);var n=l.getIdAttribute(a);q._index[this.__id=this.__clientId=a.servicePath+(this[n]||Math.random().toString(16).substring(2,14)+"@"+(g.rpc.Client&&g.rpc.Client.clientId||"client"))]=this;g.json.schema&&t&&g.json.schema.mustBeValid(g.json.schema.validate(this,a._schema));m.push({object:this,save:!0})};return p.mixin(a._constructor,a._schema,{load:a})},fetch:function(a){a=l.getServiceAndId(a);
return this.byId(a.service,a.id)},getIdAttribute:function(a){a=a._schema;var c;if(a&&!(c=a._idAttr))for(var b in a.properties)if(a.properties[b].identity||"self"==a.properties[b].link)a._idAttr=c=b;return c||"id"},getServiceAndId:function(a){var c="",b;for(b in l.services)a.substring(0,b.length)==b&&b.length>=c.length&&(c=b);if(c)return{service:l.services[c],id:a.substring(c.length)};a=a.match(/^(.*\/)([^\/]*)$/);return{service:new l.serviceClass(a[1],!0),id:a[2]}},services:{},schemas:{},registerService:function(a,
c,b){c=a.servicePath=c||a.servicePath;a._schema=l.schemas[c]=b||a._schema||{};l.services[c]=a},byId:function(a,c){var b=q._index[(a.servicePath||"")+c];return b&&!b._loadObject?(a=new p.Deferred,a.callback(b),a):this.query(a,c)},query:function(a,c,b){var d=a(c,b);d.addCallback(function(e){return e.nodeType&&e.cloneNode?e:u(a,d,e,"string"!=typeof c||b&&(b.start||b.count)?void 0:c)});return d},_loader:function(a){var c=l.getServiceAndId(this.__id),b=this;l.query(c.service,c.id).addBoth(function(c){c==
b?(delete c.$ref,delete c._loadObject):b._loadObject=function(a){a(c)};a(c)})},isDirty:function(a,c){return a?a.__isDirty:c?p.some(m,function(a){return g.data._getStoreForItem(a.object||a.old)==c}):!!m.length}};return g.rpc.JsonRest});