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