| 1 |
define(["dojo", "dojox", "require", "dojox/data/JsonQueryRestStore", "dojox/rpc/Client", "dojo/_base/url"], function(dojo, dojox, require) { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
dojox.json.ref.serializeFunctions = true; |
| 6 |
|
| 7 |
var PersevereStore = dojo.declare("dojox.data.PersevereStore", dojox.data.JsonQueryRestStore, { |
| 8 |
useFullIdInQueries: true, |
| 9 |
jsonQueryPagination: false |
| 10 |
}); |
| 11 |
|
| 12 |
PersevereStore.getStores = function( path, sync){ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
path = (path && (path.match(/\/$/) ? path : (path + '/'))) || '/'; |
| 26 |
if(path.match(/^\w*:\/\//)){ |
| 27 |
|
| 28 |
require("dojox/io/xhrScriptPlugin"); |
| 29 |
dojox.io.xhrScriptPlugin(path, "callback", dojox.io.xhrPlugins.fullHttpAdapter); |
| 30 |
} |
| 31 |
var plainXhr = dojo.xhr; |
| 32 |
dojo.xhr = function(method,args){ |
| 33 |
(args.headers = args.headers || {})['Server-Methods'] = "false"; |
| 34 |
return plainXhr.apply(dojo,arguments); |
| 35 |
}; |
| 36 |
var rootService= dojox.rpc.Rest(path,true); |
| 37 |
dojox.rpc._sync = sync; |
| 38 |
var dfd = rootService("Class/"); |
| 39 |
var results; |
| 40 |
var stores = {}; |
| 41 |
var callId = 0; |
| 42 |
dfd.addCallback(function(schemas){ |
| 43 |
dojox.json.ref.resolveJson(schemas, { |
| 44 |
index: dojox.rpc.Rest._index, |
| 45 |
idPrefix: "/Class/", |
| 46 |
assignAbsoluteIds: true |
| 47 |
}); |
| 48 |
function setupHierarchy(schema){ |
| 49 |
if(schema['extends'] && schema['extends'].prototype){ |
| 50 |
if(!schema.prototype || !schema.prototype.isPrototypeOf(schema['extends'].prototype)){ |
| 51 |
setupHierarchy(schema['extends']); |
| 52 |
dojox.rpc.Rest._index[schema.prototype.__id] = schema.prototype = dojo.mixin(dojo.delegate(schema['extends'].prototype), schema.prototype); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
function setupMethods(methodsDefinitions, methodsTarget){ |
| 57 |
if(methodsDefinitions && methodsTarget){ |
| 58 |
for(var j in methodsDefinitions){ |
| 59 |
var methodDef = methodsDefinitions[j]; |
| 60 |
|
| 61 |
|
| 62 |
if(methodDef.runAt != "client" && !methodsTarget[j]){ |
| 63 |
methodsTarget[j] = (function(methodName){ |
| 64 |
return function(){ |
| 65 |
|
| 66 |
var deferred = dojo.rawXhrPost({ |
| 67 |
url: this.__id, |
| 68 |
|
| 69 |
postData: dojox.json.ref.toJson({ |
| 70 |
method: methodName, |
| 71 |
id: callId++, |
| 72 |
params: dojo._toArray(arguments) |
| 73 |
}), |
| 74 |
handleAs: "json" |
| 75 |
}); |
| 76 |
deferred.addCallback(function(response){ |
| 77 |
|
| 78 |
return response.error ? |
| 79 |
new Error(response.error) : |
| 80 |
response.result; |
| 81 |
}); |
| 82 |
return deferred; |
| 83 |
} |
| 84 |
})(j); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
for(var i in schemas){ |
| 90 |
if(typeof schemas[i] == 'object'){ |
| 91 |
var schema = schemas[i]; |
| 92 |
setupHierarchy(schema); |
| 93 |
setupMethods(schema.methods, schema.prototype = schema.prototype || {}); |
| 94 |
setupMethods(schema.staticMethods, schema); |
| 95 |
stores[schemas[i].id] = new dojox.data.PersevereStore({target:new dojo._Url(path,schemas[i].id) + '/',schema:schema}); |
| 96 |
} |
| 97 |
} |
| 98 |
return (results = stores); |
| 99 |
}); |
| 100 |
dojo.xhr = plainXhr; |
| 101 |
return sync ? results : dfd; |
| 102 |
}; |
| 103 |
PersevereStore.addProxy = function(){ |
| 104 |
|
| 105 |
|
| 106 |
require("dojox/io/xhrPlugins"); |
| 107 |
dojox.io.xhrPlugins.addProxy("/proxy/"); |
| 108 |
}; |
| 109 |
|
| 110 |
return PersevereStore; |
| 111 |
|
| 112 |
}); |