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");
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
14 isItemLoaded: function(/*object*/ item) {
16 // Overload of the isItemLoaded function to look for items of type 'stub', which indicate
17 // the data hasn't been loaded in yet.
20 // The item to examine.
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"){
30 loadItem: function(keywordArgs){
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.
38 // The item to examine.
40 var item = keywordArgs.item;
41 this._assertIsItem(item);
43 //Build the path to the data.json for this item
44 //The path consists of where its parent was loaded from
46 var itemName = this.getValue(item, "name");
47 var parent = this.getValue(item, "parent");
50 dataUrl += (parent + "/");
53 //For this store, all child input data is loaded from a url that ends with data.json
54 dataUrl += itemName + "/data.json";
56 //Need a reference to the store to call back to its structures.
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
68 //Clear off the stub indicators.
72 //Set up the loaded values in the format ItemFileReadStore uses for attributes.
74 if (dojo.isArray(data[i])) {
81 //Reset the item in the reference.
82 self._arrayOfAllItems[item[self._itemNumPropName]] = item;
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);
88 var values = self.getValues(item, attributes[i]);
89 for (var j = 0; j < values.length; j++) {
90 var value = values[j];
92 if(typeof value === "object"){
94 //We have a stub reference here, we need to create the stub item
97 name: [value["stub"]], //
98 parent: [itemName] //The child stub item is parented by this item name...
101 //Add in any parents to your parent so URL construstruction is accurate.
102 stub.parent[0] = parent + "/" + stub.parent[0];
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.
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);
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);
130 //Fire the get and pass the proper callbacks to the deferred.
133 handleAs: "json-comment-optional"
135 var d = dojo.xhrGet(xhrArgs);
136 d.addCallback(gotData);
137 d.addErrback(gotError);