PersevereStore.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. define("dojox/data/PersevereStore", ["dojo", "dojox", "require", "dojox/data/JsonQueryRestStore", "dojox/rpc/Client", "dojo/_base/url"], function(dojo, dojox, require) {
  2. // PersevereStore is an extension of JsonRestStore to handle Persevere's special features
  3. dojox.json.ref.serializeFunctions = true; // Persevere supports persisted functions
  4. dojo.declare("dojox.data.PersevereStore",dojox.data.JsonQueryRestStore,{
  5. useFullIdInQueries: true, // in JSONQuerys use the full id
  6. jsonQueryPagination: false // use the Range headers instead
  7. });
  8. dojox.data.PersevereStore.getStores = function(/*String?*/path,/*Boolean?*/sync){
  9. // summary:
  10. // Creates Dojo data stores for all the table/classes on a Persevere server
  11. // path:
  12. // URL of the Persevere server's root, this normally just "/"
  13. // which is the default value if the target is not provided
  14. // sync:
  15. // Indicates that the operation should happen synchronously.
  16. // return:
  17. // A map/object of datastores will be returned if it is performed asynchronously,
  18. // otherwise it will return a Deferred object that will provide the map/object.
  19. // The name of each property is a the name of a store,
  20. // and the value is the actual data store object.
  21. path = (path && (path.match(/\/$/) ? path : (path + '/'))) || '/';
  22. if(path.match(/^\w*:\/\//)){
  23. // if it is cross-domain, we will use window.name for communication
  24. require("dojox/io/xhrScriptPlugin");
  25. dojox.io.xhrScriptPlugin(path, "callback", dojox.io.xhrPlugins.fullHttpAdapter);
  26. }
  27. var plainXhr = dojo.xhr;
  28. dojo.xhr = function(method,args){
  29. (args.headers = args.headers || {})['Server-Methods'] = "false";
  30. return plainXhr.apply(dojo,arguments);
  31. }
  32. var rootService= dojox.rpc.Rest(path,true);
  33. dojox.rpc._sync = sync;
  34. var dfd = rootService("Class/");//dojo.xhrGet({url: target, sync:!callback, handleAs:'json'});
  35. var results;
  36. var stores = {};
  37. var callId = 0;
  38. dfd.addCallback(function(schemas){
  39. dojox.json.ref.resolveJson(schemas, {
  40. index: dojox.rpc.Rest._index,
  41. idPrefix: "/Class/",
  42. assignAbsoluteIds: true
  43. });
  44. function setupHierarchy(schema){
  45. if(schema['extends'] && schema['extends'].prototype){
  46. if(!schema.prototype || !schema.prototype.isPrototypeOf(schema['extends'].prototype)){
  47. setupHierarchy(schema['extends']);
  48. dojox.rpc.Rest._index[schema.prototype.__id] = schema.prototype = dojo.mixin(dojo.delegate(schema['extends'].prototype), schema.prototype);
  49. }
  50. }
  51. }
  52. function setupMethods(methodsDefinitions, methodsTarget){
  53. if(methodsDefinitions && methodsTarget){
  54. for(var j in methodsDefinitions){
  55. var methodDef = methodsDefinitions[j];
  56. // if any method definitions indicate that the method should run on the server, than add
  57. // it to the prototype as a JSON-RPC method
  58. if(methodDef.runAt != "client" && !methodsTarget[j]){
  59. methodsTarget[j] = (function(methodName){
  60. return function(){
  61. // execute a JSON-RPC call
  62. var deferred = dojo.rawXhrPost({
  63. url: this.__id,
  64. // the JSON-RPC call
  65. postData: dojox.json.ref.toJson({
  66. method: methodName,
  67. id: callId++,
  68. params: dojo._toArray(arguments)
  69. }),
  70. handleAs: "json"
  71. });
  72. deferred.addCallback(function(response){
  73. // handle the response
  74. return response.error ?
  75. new Error(response.error) :
  76. response.result;
  77. });
  78. return deferred;
  79. }
  80. })(j);
  81. }
  82. }
  83. }
  84. }
  85. for(var i in schemas){
  86. if(typeof schemas[i] == 'object'){
  87. var schema = schemas[i];
  88. setupHierarchy(schema);
  89. setupMethods(schema.methods, schema.prototype = schema.prototype || {});
  90. setupMethods(schema.staticMethods, schema);
  91. stores[schemas[i].id] = new dojox.data.PersevereStore({target:new dojo._Url(path,schemas[i].id) + '/',schema:schema});
  92. }
  93. }
  94. return (results = stores);
  95. });
  96. dojo.xhr = plainXhr;
  97. return sync ? results : dfd;
  98. };
  99. dojox.data.PersevereStore.addProxy = function(){
  100. // summary:
  101. // Invokes the XHR proxy plugin. Call this if you will be using x-site data.
  102. require("dojox/io/xhrPlugins"); // also not necessary, but we can register that Persevere supports proxying
  103. dojox.io.xhrPlugins.addProxy("/proxy/");
  104. };
  105. return dojox.data.PersevereStore;
  106. });