df65191c01eaf39f33e181a2102da1ba39a26ad4.svn-base 6.0 KB

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