]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/data/demos/stores/LazyLoadJSIStore.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / data / demos / stores / LazyLoadJSIStore.js
1 if(!dojo._hasResource["dojox.data.demos.stores.LazyLoadJSIStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.demos.stores.LazyLoadJSIStore"] = true;
3 dojo.provide("dojox.data.demos.stores.LazyLoadJSIStore");
4 dojo.require("dojo.data.ItemFileReadStore");
5
6 dojo.declare("dojox.data.demos.stores.LazyLoadJSIStore", dojo.data.ItemFileReadStore, {
7         constructor: function(/* object */ keywordParameters){
8                 // LazyLoadJSIStore extends ItemFileReadStore to implement an 
9                 // example of lazy-loading/faulting in items on-demand.
10                 // Note this is certianly not a perfect implementation, it is 
11                 // an example.
12         },
13         
14         isItemLoaded: function(/*object*/ item) {
15                 //      summary:
16                 //              Overload of the isItemLoaded function to look for items of type 'stub', which indicate
17                 //              the data hasn't been loaded in yet.
18                 //
19                 //      item:
20                 //              The item to examine.
21                 
22                 //For this store, if it has the value of stub for its type attribute, 
23                 //then the item basn't been fully loaded yet.  It's just a placeholder.
24                 if(this.getValue(item, "type") === "stub"){
25                         return false;
26                 }
27                 return true;
28         },
29                 
30         loadItem: function(keywordArgs){
31                 //      summary:
32                 //              Overload of the loadItem function to fault in items.  This assumes the data for an item is laid out
33                 //              in a RESTful sort of pattern name0/name1/data.json and so on and uses that to load the data.
34                 //              It will also detect stub items in the newly loaded item and insert the stubs into the ItemFileReadStore
35                 //              list so they can also be loaded in on-demand.
36                 //
37                 //      item:
38                 //              The item to examine.
39
40                 var item = keywordArgs.item;
41                 this._assertIsItem(item);
42
43                 //Build the path to the data.json for this item
44                 //The path consists of where its parent was loaded from 
45                 //plus the item name.
46                 var itemName = this.getValue(item, "name");
47                 var parent   = this.getValue(item, "parent");
48                 var dataUrl  = "";
49                 if (parent){
50                         dataUrl += (parent + "/");
51                 }
52
53                 //For this store, all child input data is loaded from a url that ends with data.json
54                 dataUrl += itemName + "/data.json";
55
56                 //Need a reference to the store to call back to its structures.
57                 var self = this;
58
59                 // Callback for handling a successful load.
60                 var gotData = function(data){
61                         //Now we need to modify the existing item a bit to take it out of stub state
62                         //Since we extend the store and have knowledge of the internal
63                         //structure, this can be done here.  Now, is we extended
64                         //a write store, we could call the write APIs to do this too
65                         //But for a simple demo the diretc modification in the store function
66                         //is sufficient.
67
68                         //Clear off the stub indicators.
69                         delete item.type;
70                         delete item.parent;
71
72                         //Set up the loaded values in the format ItemFileReadStore uses for attributes.
73                         for (i in data) {
74                                 if (dojo.isArray(data[i])) {
75                                         item[i] = data[i];
76                                 }else{
77                                         item[i] = [data[i]];
78                                 }
79                         }
80
81                         //Reset the item in the reference.
82                         self._arrayOfAllItems[item[self._itemNumPropName]] = item;
83
84                         //Scan the new values in the item for extra stub items we need to 
85                         //add to the items array of the store so they can be lazy-loaded later...
86                         var attributes = self.getAttributes(item);
87                         for(i in attributes){
88                                 var values = self.getValues(item, attributes[i]);
89                                 for (var j = 0; j < values.length; j++) {
90                                         var value = values[j];
91                                         
92                                         if(typeof value === "object"){
93                                                 if(value["stub"] ){
94                                                         //We have a stub reference here, we need to create the stub item
95                                                         var stub = {
96                                                                 type: ["stub"],
97                                                                 name: [value["stub"]],  //
98                                                                 parent: [itemName]              //The child stub item is parented by this item name...
99                                                         };
100                                                         if (parent) {
101                                                                 //Add in any parents to your parent so URL construstruction is accurate.
102                                                                 stub.parent[0] = parent + "/" + stub.parent[0]; 
103                                                         }
104                                                         //Finalize the addition of the new stub item into the ItemFileReadStore list.
105                                                         self._arrayOfAllItems.push(stub);
106                                                         stub[self._storeRefPropName] = self;
107                                                         stub[self._itemNumPropName] = (self._arrayOfAllItems.length - 1); //Last one pushed in should be the item
108                                                         values[j] = stub; //Set the stub item back in its place and replace the stub notation.
109                                                 }
110                                         }
111                                 }
112                         }
113
114                         //Done processing!  Call the onItem, if any.
115                         if(keywordArgs.onItem){
116                                 var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
117                                 keywordArgs.onItem.call(scope, item);
118                         }
119                 };
120
121                 //Callback for any errors that occur during load.
122                 var gotError = function(error){
123                         //Call the onComplete, if any
124                         if(keywordArgs.onError){
125                                 var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
126                                 keywordArgs.onError.call(scope, error);
127                         }
128                 };
129
130                 //Fire the get and pass the proper callbacks to the deferred.
131                 var xhrArgs = {
132                         url: dataUrl,
133                         handleAs: "json-comment-optional"
134                 };
135                 var d = dojo.xhrGet(xhrArgs);
136                 d.addCallback(gotData);
137                 d.addErrback(gotError);
138         }
139 });
140
141
142 }