source: Dev/trunk/src/client/dojox/data/PersevereStore.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.2 KB
Line 
1define(["dojo", "dojox", "require", "dojox/data/JsonQueryRestStore", "dojox/rpc/Client", "dojo/_base/url"], function(dojo, dojox, require) {
2
3// PersevereStore is an extension of JsonRestStore to handle Persevere's special features
4
5dojox.json.ref.serializeFunctions = true; // Persevere supports persisted functions
6
7var PersevereStore = dojo.declare("dojox.data.PersevereStore", dojox.data.JsonQueryRestStore, {
8        useFullIdInQueries: true, // in JSONQuerys use the full id
9        jsonQueryPagination: false // use the Range headers instead
10});
11
12PersevereStore.getStores = function(/*String?*/ path,/*Boolean?*/ sync){
13        // summary:
14        //              Creates Dojo data stores for all the table/classes on a Persevere server
15        // path:
16        //              URL of the Persevere server's root, this normally just "/"
17        //              which is the default value if the target is not provided
18        // sync:
19        //              Indicates that the operation should happen synchronously.
20        // returns:
21        //              A map/object of datastores will be returned if it is performed asynchronously,
22        //              otherwise it will return a Deferred object that will provide the map/object.
23        //              The name of each property is a the name of a store,
24        //              and the value is the actual data store object.
25        path = (path && (path.match(/\/$/) ? path : (path + '/'))) || '/';
26        if(path.match(/^\w*:\/\//)){
27                // if it is cross-domain, we will use window.name for communication
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/");//dojo.xhrGet({url: target, sync:!callback, handleAs:'json'});
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                                        // if any method definitions indicate that the method should run on the server, than add
61                                        // it to the prototype as a JSON-RPC method
62                                        if(methodDef.runAt != "client" && !methodsTarget[j]){
63                                                methodsTarget[j] = (function(methodName){
64                                                        return function(){
65                                                                // execute a JSON-RPC call
66                                                                var deferred = dojo.rawXhrPost({
67                                                                        url: this.__id,
68                                                                        // the JSON-RPC call
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                                                                        // handle the response
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};
103PersevereStore.addProxy = function(){
104        // summary:
105        //              Invokes the XHR proxy plugin. Call this if you will be using x-site data.
106        require("dojox/io/xhrPlugins"); // also not necessary, but we can register that Persevere supports proxying
107        dojox.io.xhrPlugins.addProxy("/proxy/");
108};
109
110return PersevereStore;
111
112});
Note: See TracBrowser for help on using the repository browser.