QueryReadStore.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. if (!dojo._hasResource["dojox.data.QueryReadStore"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.data.QueryReadStore"] = true;
  7. dojo.provide("dojox.data.QueryReadStore");
  8. dojo.provide("dojox.data.QueryReadStore.InvalidItemError");
  9. dojo.provide("dojox.data.QueryReadStore.InvalidAttributeError");
  10. dojo.require("dojo.string");
  11. dojo.require("dojo.data.util.simpleFetch");
  12. dojo.declare("dojox.data.QueryReadStore", null, {
  13. /*
  14. * // summary: // This class provides a store that is mainly intended to
  15. * be used // for loading data dynamically from the server, used i.e.
  16. * for // retreiving chunks of data from huge data stores on the server
  17. * (by server-side filtering!). // Upon calling the fetch() method of
  18. * this store the data are requested from // the server if they are not
  19. * yet loaded for paging (or cached). // // For example used for a
  20. * combobox which works on lots of data. It // can be used to retreive
  21. * the data partially upon entering the // letters "ac" it returns only
  22. * items like "action", "acting", etc. // // note: // The field name
  23. * "id" in a query is reserved for looking up data // by id. This is
  24. * necessary as before the first fetch, the store // has no way of
  25. * knowing which field the server will declare as // identifier. // //
  26. * examples: // // The parameter "query" contains the data that are sent
  27. * to the server. // var store = new
  28. * dojox.data.QueryReadStore({url:'/search.php'}); //
  29. * store.fetch({query:{name:'a'}, queryOptions:{ignoreCase:false}}); // // //
  30. * Since "serverQuery" is given, it overrules and those data are // //
  31. * sent to the server. // var store = new
  32. * dojox.data.QueryReadStore({url:'/search.php'}); //
  33. * store.fetch({serverQuery:{name:'a'},
  34. * queryOptions:{ignoreCase:false}}); // // todo: // - there is a bug in
  35. * the paging, when i set start:2, count:5 after an initial fetch() and
  36. * doClientPaging:true // it returns 6 elemetns, though count=5, try it
  37. * in QueryReadStore.html // - allow configuring if the paging shall
  38. * takes place on the client or the server // - add optional caching // -
  39. * when the first query searched for "a" and the next for a subset of //
  40. * the first, i.e. "ab" then we actually dont need a server request, if //
  41. * we have client paging, we just need to filter the items we already
  42. * have // that might also be tooo much logic
  43. */
  44. url : "",
  45. requestMethod : "get",
  46. // useCache:false,
  47. // We use the name in the errors, once the name is fixed hardcode it,
  48. // may be.
  49. _className : "dojox.data.QueryReadStore",
  50. // This will contain the items we have loaded from the server.
  51. // The contents of this array is optimized to satisfy all read-api
  52. // requirements
  53. // and for using lesser storage, so the keys and their content need some
  54. // explaination:
  55. // this._items[0].i - the item itself
  56. // this._items[0].r - a reference to the store, so we can identify the
  57. // item
  58. // securly. We set this reference right after receiving the item from
  59. // the
  60. // server.
  61. _items : [],
  62. // Store the last query that triggered xhr request to the server.
  63. // So we can compare if the request changed and if we shall reload
  64. // (this also depends on other factors, such as is caching used, etc).
  65. _lastServerQuery : null,
  66. // Store a hash of the last server request. Actually I introduced this
  67. // for testing, so I can check if no unnecessary requests were issued
  68. // for
  69. // client-side-paging.
  70. lastRequestHash : null,
  71. // If this is false, every request is sent to the server.
  72. // If it's true a second request with the same query will not issue
  73. // another
  74. // request, but use the already returned data. This assumes that the
  75. // server
  76. // does not do the paging.
  77. doClientPaging : true,
  78. // Items by identify for Identify API
  79. _itemsByIdentity : null,
  80. // Identifier used
  81. _identifier : null,
  82. _features : {
  83. 'dojo.data.api.Read' : true,
  84. 'dojo.data.api.Identity' : true
  85. },
  86. constructor : function(/* Object */params) {
  87. dojo.mixin(this, params);
  88. },
  89. getValue : function(/* item */item, /* attribute-name-string */
  90. attribute, /* value? */defaultValue) {
  91. // According to the Read API comments in getValue() and exception is
  92. // thrown when an item is not an item or the attribute not a string!
  93. this._assertIsItem(item);
  94. if (!dojo.isString(attribute)) {
  95. throw new Error(this._className
  96. + ".getValue(): Invalid attribute, string expected!");
  97. }
  98. if (!this.hasAttribute(item, attribute)) {
  99. // read api says: return defaultValue "only if *item* does not
  100. // have a value for *attribute*."
  101. // Is this the case here? The attribute doesn't exist, but a
  102. // defaultValue, sounds reasonable.
  103. if (defaultValue) {
  104. return defaultValue;
  105. }
  106. console.log(this._className
  107. + ".getValue(): Item does not have the attribute '"
  108. + attribute + "'.");
  109. }
  110. return item.i[attribute];
  111. },
  112. getValues : function(/* item */item, /* attribute-name-string */
  113. attribute) {
  114. var ret = [];
  115. if (this.hasAttribute(item, attribute)) {
  116. ret.push(item.i[attribute]);
  117. }
  118. return ret;
  119. },
  120. getAttributes : function(/* item */item) {
  121. this._assertIsItem(item);
  122. var ret = [];
  123. for (var i in item.i) {
  124. ret.push(i);
  125. }
  126. return ret;
  127. },
  128. hasAttribute : function(/* item */item, /* attribute-name-string */
  129. attribute) {
  130. // summary:
  131. // See dojo.data.api.Read.hasAttribute()
  132. return this.isItem(item) && typeof item.i[attribute] != "undefined";
  133. },
  134. containsValue : function(/* item */item, /* attribute-name-string */
  135. attribute, /* anything */value) {
  136. var values = this.getValues(item, attribute);
  137. var len = values.length;
  138. for (var i = 0; i < len; i++) {
  139. if (values[i] == value) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. },
  145. isItem : function(/* anything */something) {
  146. // Some basic tests, that are quick and easy to do here.
  147. // >>> var store = new dojox.data.QueryReadStore({});
  148. // >>> store.isItem("");
  149. // false
  150. //
  151. // >>> var store = new dojox.data.QueryReadStore({});
  152. // >>> store.isItem({});
  153. // false
  154. //
  155. // >>> var store = new dojox.data.QueryReadStore({});
  156. // >>> store.isItem(0);
  157. // false
  158. //
  159. // >>> var store = new dojox.data.QueryReadStore({});
  160. // >>> store.isItem({name:"me", label:"me too"});
  161. // false
  162. //
  163. if (something) {
  164. return typeof something.r != "undefined" && something.r == this;
  165. }
  166. return false;
  167. },
  168. isItemLoaded : function(/* anything */something) {
  169. // Currently we dont have any state that tells if an item is loaded
  170. // or not
  171. // if the item exists its also loaded.
  172. // This might change when we start working with refs inside items
  173. // ...
  174. return this.isItem(something);
  175. },
  176. loadItem : function(/* object */args) {
  177. if (this.isItemLoaded(args.item)) {
  178. return;
  179. }
  180. // Actually we have nothing to do here, or at least I dont know what
  181. // to do here ...
  182. },
  183. fetch : function(/* Object? */request) {
  184. // summary:
  185. // See dojo.data.util.simpleFetch.fetch() this is just a copy and I
  186. // adjusted
  187. // only the paging, since it happens on the server if doClientPaging
  188. // is
  189. // false, thx to http://trac.dojotoolkit.org/ticket/4761 reporting
  190. // this.
  191. // Would be nice to be able to use simpleFetch() to reduce copied
  192. // code,
  193. // but i dont know how yet. Ideas please!
  194. request = request || {};
  195. if (!request.store) {
  196. request.store = this;
  197. }
  198. var self = this;
  199. var _errorHandler = function(errorData, requestObject) {
  200. if (requestObject.onError) {
  201. var scope = requestObject.scope || dojo.global;
  202. requestObject.onError.call(scope, errorData, requestObject);
  203. }
  204. };
  205. var _fetchHandler = function(items, requestObject) {
  206. var oldAbortFunction = requestObject.abort || null;
  207. var aborted = false;
  208. var startIndex = requestObject.start ? requestObject.start : 0;
  209. if (self.doClientPaging == false) {
  210. // For client paging we dont need no slicing of the result.
  211. startIndex = 0;
  212. }
  213. var endIndex = requestObject.count
  214. ? (startIndex + requestObject.count)
  215. : items.length;
  216. requestObject.abort = function() {
  217. aborted = true;
  218. if (oldAbortFunction) {
  219. oldAbortFunction.call(requestObject);
  220. }
  221. };
  222. var scope = requestObject.scope || dojo.global;
  223. if (!requestObject.store) {
  224. requestObject.store = self;
  225. }
  226. if (requestObject.onBegin) {
  227. requestObject.onBegin.call(scope, items.length,
  228. requestObject);
  229. }
  230. if (requestObject.sort) {
  231. items.sort(dojo.data.util.sorter.createSortFunction(
  232. requestObject.sort, self));
  233. }
  234. if (requestObject.onItem) {
  235. for (var i = startIndex; (i < items.length)
  236. && (i < endIndex); ++i) {
  237. var item = items[i];
  238. if (!aborted) {
  239. requestObject.onItem.call(scope, item,
  240. requestObject);
  241. }
  242. }
  243. }
  244. if (requestObject.onComplete && !aborted) {
  245. var subset = null;
  246. if (!requestObject.onItem) {
  247. subset = items.slice(startIndex, endIndex);
  248. }
  249. requestObject.onComplete.call(scope, subset, requestObject);
  250. }
  251. };
  252. this._fetchItems(request, _fetchHandler, _errorHandler);
  253. return request; // Object
  254. },
  255. getFeatures : function() {
  256. return this._features;
  257. },
  258. close : function(
  259. /* dojo.data.api.Request || keywordArgs || null */request) {
  260. // I have no idea if this is really needed ...
  261. },
  262. getLabel : function(/* item */item) {
  263. // Override it to return whatever the label shall be, see Read-API.
  264. return undefined;
  265. },
  266. getLabelAttributes : function(/* item */item) {
  267. return null;
  268. },
  269. _fetchItems : function(request, fetchHandler, errorHandler) {
  270. // summary:
  271. // The request contains the data as defined in the Read-API.
  272. // Additionally there is following keyword "serverQuery".
  273. //
  274. // The *serverQuery* parameter, optional.
  275. // This parameter contains the data that will be sent to the server.
  276. // If this parameter is not given the parameter "query"'s
  277. // data are sent to the server. This is done for some reasons:
  278. // - to specify explicitly which data are sent to the server, they
  279. // might also be a mix of what is contained in "query",
  280. // "queryOptions"
  281. // and the paging parameters "start" and "count" or may be even
  282. // completely different things.
  283. // - don't modify the request.query data, so the interface using
  284. // this
  285. // store can rely on unmodified data, as the combobox dijit
  286. // currently
  287. // does it, it compares if the query has changed
  288. // - request.query is required by the Read-API
  289. //
  290. // I.e. the following examples might be sent via GET:
  291. // fetch({query:{name:"abc"}, queryOptions:{ignoreCase:true}})
  292. // the URL will become: /url.php?name=abc
  293. //
  294. // fetch({serverQuery:{q:"abc", c:true}, query:{name:"abc"},
  295. // queryOptions:{ignoreCase:true}})
  296. // the URL will become: /url.php?q=abc&c=true
  297. // // The serverQuery-parameter has overruled the query-parameter
  298. // // but the query parameter stays untouched, but is not sent to
  299. // the server!
  300. // // The serverQuery contains more data than the query, so they
  301. // might differ!
  302. //
  303. var serverQuery = request.serverQuery || request.query || {};
  304. // Need to add start and count
  305. if (!this.doClientPaging) {
  306. serverQuery.start = request.start || 0;
  307. // Count might not be sent if not given.
  308. if (request.count) {
  309. serverQuery.count = request.count;
  310. }
  311. }
  312. // Compare the last query and the current query by simply
  313. // json-encoding them,
  314. // so we dont have to do any deep object compare ... is there some
  315. // dojo.areObjectsEqual()???
  316. if (this.doClientPaging
  317. && this._lastServerQuery !== null
  318. && dojo.toJson(serverQuery) == dojo
  319. .toJson(this._lastServerQuery)) {
  320. fetchHandler(this._items, request);
  321. } else {
  322. var xhrFunc = this.requestMethod.toLowerCase() == "post"
  323. ? dojo.xhrPost
  324. : dojo.xhrGet;
  325. var xhrHandler = xhrFunc({
  326. url : this.url,
  327. handleAs : "json-comment-optional",
  328. content : serverQuery
  329. });
  330. xhrHandler.addCallback(dojo.hitch(this, function(data) {
  331. data = this._filterResponse(data);
  332. this._items = [];
  333. // Store a ref to "this" in each item, so we can simply
  334. // check if an item
  335. // really origins form here (idea is from ItemFileReadStore,
  336. // I just don't know
  337. // how efficient the real storage use, garbage collection
  338. // effort, etc. is).
  339. dojo.forEach(data.items, function(e) {
  340. this._items.push({
  341. i : e,
  342. r : this
  343. });
  344. }, this);
  345. var identifier = data.identifier;
  346. this._itemsByIdentity = {};
  347. if (identifier) {
  348. this._identifier = identifier;
  349. for (i = 0; i < this._items.length; ++i) {
  350. var item = this._items[i].i;
  351. var identity = item[identifier];
  352. if (!this._itemsByIdentity[identity]) {
  353. this._itemsByIdentity[identity] = item;
  354. } else {
  355. throw new Error("dojo.data.QueryReadStore: The json data as specified by: ["
  356. + this.url
  357. + "] is malformed. Items within the list have identifier: ["
  358. + identifier
  359. + "]. Value collided: ["
  360. + identity + "]");
  361. }
  362. }
  363. } else {
  364. this._identifier = Number;
  365. for (i = 0; i < this._items.length; ++i) {
  366. this._items[i].n = i;
  367. }
  368. }
  369. // TODO actually we should do the same as
  370. // dojo.data.ItemFileReadStore._getItemsFromLoadedData() to
  371. // sanitize
  372. // (does it really sanititze them) and store the data
  373. // optimal. should we? for security reasons???
  374. fetchHandler(this._items, request);
  375. }));
  376. xhrHandler.addErrback(function(error) {
  377. errorHandler(error, request);
  378. });
  379. // Generate the hash using the time in milliseconds and a randon
  380. // number.
  381. // Since Math.randon() returns something like: 0.23453463, we
  382. // just remove the "0."
  383. // probably just for esthetic reasons :-).
  384. this.lastRequestHash = allGetServerTime().getTime() + "-"
  385. + String(Math.random()).substring(2);
  386. this._lastServerQuery = dojo.mixin({}, serverQuery);
  387. }
  388. },
  389. _filterResponse : function(data) {
  390. // summary:
  391. // If the data from servers needs to be processed before it can be
  392. // processed by this
  393. // store, then this function should be re-implemented in subclass.
  394. // This default
  395. // implementation just return the data unchanged.
  396. // data:
  397. // The data received from server
  398. return data;
  399. },
  400. _assertIsItem : function(/* item */item) {
  401. // summary:
  402. // It throws an error if item is not valid, so you can call it in
  403. // every method that needs to
  404. // throw an error when item is invalid.
  405. // item:
  406. // The item to test for being contained by the store.
  407. if (!this.isItem(item)) {
  408. throw new dojox.data.QueryReadStore.InvalidItemError(this._className
  409. + ": a function was passed an item argument that was not an item");
  410. }
  411. },
  412. _assertIsAttribute : function(/* attribute-name-string */attribute) {
  413. // summary:
  414. // This function tests whether the item passed in is indeed a valid
  415. // 'attribute' like type for the store.
  416. // attribute:
  417. // The attribute to test for being contained by the store.
  418. if (typeof attribute !== "string") {
  419. throw new dojox.data.QueryReadStore.InvalidAttributeError(this._className
  420. + ": '"
  421. + attribute
  422. + "' is not a valid attribute identifier.");
  423. }
  424. },
  425. fetchItemByIdentity : function(/* Object */keywordArgs) {
  426. // summary:
  427. // See dojo.data.api.Identity.fetchItemByIdentity()
  428. // See if we have already loaded the item with that id
  429. // In case there hasn't been a fetch yet, _itemsByIdentity is null
  430. // and thus a fetch will be triggered below.
  431. if (this._itemsByIdentity) {
  432. var item = this._itemsByIdentity[keywordArgs.identity];
  433. if (!(item === undefined)) {
  434. if (keywordArgs.onItem) {
  435. var scope = keywordArgs.scope
  436. ? keywordArgs.scope
  437. : dojo.global;
  438. keywordArgs.onItem.call(scope, {
  439. i : item,
  440. r : this
  441. });
  442. }
  443. return;
  444. }
  445. }
  446. // Otherwise we need to go remote
  447. // Set up error handler
  448. var _errorHandler = function(errorData, requestObject) {
  449. var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
  450. if (keywordArgs.onError) {
  451. keywordArgs.onError.call(scope, error);
  452. }
  453. };
  454. // Set up fetch handler
  455. var _fetchHandler = function(items, requestObject) {
  456. var scope = keywordArgs.scope ? keywordArgs.scope : dojo.global;
  457. try {
  458. // There is supposed to be only one result
  459. var item = null;
  460. if (items && items.length == 1) {
  461. item = items[0];
  462. }
  463. // If no item was found, item is still null and we'll
  464. // fire the onItem event with the null here
  465. if (keywordArgs.onItem) {
  466. keywordArgs.onItem.call(scope, item);
  467. }
  468. } catch (error) {
  469. if (keywordArgs.onError) {
  470. keywordArgs.onError.call(scope, error);
  471. }
  472. }
  473. };
  474. // Construct query
  475. var request = {
  476. serverQuery : {
  477. id : keywordArgs.identity
  478. }
  479. };
  480. // Dispatch query
  481. this._fetchItems(request, _fetchHandler, _errorHandler);
  482. },
  483. getIdentity : function(/* item */item) {
  484. // summary:
  485. // See dojo.data.api.Identity.getIdentity()
  486. var identifier = null;
  487. if (this._identifier === Number) {
  488. identifier = item.n; // Number
  489. } else {
  490. identifier = item.i[this._identifier];
  491. }
  492. return identifier;
  493. },
  494. getIdentityAttributes : function(/* item */item) {
  495. // summary:
  496. // See dojo.data.api.Identity.getIdentityAttributes()
  497. return [this._identifier];
  498. }
  499. });
  500. dojo.declare("dojox.data.QueryReadStore.InvalidItemError", Error, {});
  501. dojo.declare("dojox.data.QueryReadStore.InvalidAttributeError", Error, {});
  502. }