PersevereStore.js 4.5 KB

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