Read.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. define("dojo/data/api/Read", ["../..", "./Request"], function(dojo) {
  2. // module:
  3. // dojo/data/api/Read
  4. // summary:
  5. // TODOC
  6. dojo.declare("dojo.data.api.Read", null, {
  7. // summary:
  8. // This is an abstract API that data provider implementations conform to.
  9. // This file defines methods signatures and intentionally leaves all the
  10. // methods unimplemented. For more information on the dojo.data APIs,
  11. // please visit: http://www.dojotoolkit.org/node/98
  12. getValue: function( /* item */ item,
  13. /* attribute-name-string */ attribute,
  14. /* value? */ defaultValue){
  15. // summary:
  16. // Returns a single attribute value.
  17. // Returns defaultValue if and only if *item* does not have a value for *attribute*.
  18. // Returns null if and only if null was explicitly set as the attribute value.
  19. // Returns undefined if and only if the item does not have a value for the
  20. // given attribute (which is the same as saying the item does not have the attribute).
  21. // description:
  22. // Saying that an "item x does not have a value for an attribute y"
  23. // is identical to saying that an "item x does not have attribute y".
  24. // It is an oxymoron to say "that attribute is present but has no values"
  25. // or "the item has that attribute but does not have any attribute values".
  26. // If store.hasAttribute(item, attribute) returns false, then
  27. // store.getValue(item, attribute) will return undefined.
  28. // item:
  29. // The item to access values on.
  30. // attribute:
  31. // The attribute to access represented as a string.
  32. // defaultValue:
  33. // Optional. A default value to use for the getValue return in the attribute does not exist or has no value.
  34. // returns:
  35. // a literal, an item, null, or undefined (never an array)
  36. // exceptions:
  37. // Throws an exception if *item* is not an item, or *attribute* is not a string
  38. // example:
  39. // | var darthVader = store.getValue(lukeSkywalker, "father");
  40. throw new Error('Unimplemented API: dojo.data.api.Read.getValue');
  41. },
  42. getValues: function(/* item */ item,
  43. /* attribute-name-string */ attribute){
  44. // summary:
  45. // This getValues() method works just like the getValue() method, but getValues()
  46. // always returns an array rather than a single attribute value. The array
  47. // may be empty, may contain a single attribute value, or may contain
  48. // many attribute values.
  49. // If the item does not have a value for the given attribute, then getValues()
  50. // will return an empty array: []. (So, if store.hasAttribute(item, attribute)
  51. // has a return of false, then store.getValues(item, attribute) will return [].)
  52. // item:
  53. // The item to access values on.
  54. // attribute:
  55. // The attribute to access represented as a string.
  56. // returns:
  57. // an array that may contain literals and items
  58. // exceptions:
  59. // Throws an exception if *item* is not an item, or *attribute* is not a string
  60. // example:
  61. // | var friendsOfLuke = store.getValues(lukeSkywalker, "friends");
  62. throw new Error('Unimplemented API: dojo.data.api.Read.getValues');
  63. },
  64. getAttributes: function(/* item */ item){
  65. // summary:
  66. // Returns an array with all the attributes that this item has. This
  67. // method will always return an array; if the item has no attributes
  68. // at all, getAttributes() will return an empty array: [].
  69. // item:
  70. // The item to access attributes on.
  71. // exceptions:
  72. // Throws an exception if *item* is not an item, or *attribute* is not a string
  73. // example:
  74. // | var array = store.getAttributes(kermit);
  75. throw new Error('Unimplemented API: dojo.data.api.Read.getAttributes');
  76. },
  77. hasAttribute: function( /* item */ item,
  78. /* attribute-name-string */ attribute){
  79. // summary:
  80. // Returns true if the given *item* has a value for the given *attribute*.
  81. // item:
  82. // The item to access attributes on.
  83. // attribute:
  84. // The attribute to access represented as a string.
  85. // exceptions:
  86. // Throws an exception if *item* is not an item, or *attribute* is not a string
  87. // example:
  88. // | var trueOrFalse = store.hasAttribute(kermit, "color");
  89. throw new Error('Unimplemented API: dojo.data.api.Read.hasAttribute');
  90. },
  91. containsValue: function(/* item */ item,
  92. /* attribute-name-string */ attribute,
  93. /* anything */ value){
  94. // summary:
  95. // Returns true if the given *value* is one of the values that getValues()
  96. // would return.
  97. // item:
  98. // The item to access values on.
  99. // attribute:
  100. // The attribute to access represented as a string.
  101. // value:
  102. // The value to match as a value for the attribute.
  103. // exceptions:
  104. // Throws an exception if *item* is not an item, or *attribute* is not a string
  105. // example:
  106. // | var trueOrFalse = store.containsValue(kermit, "color", "green");
  107. throw new Error('Unimplemented API: dojo.data.api.Read.containsValue');
  108. },
  109. isItem: function(/* anything */ something){
  110. // summary:
  111. // Returns true if *something* is an item and came from the store instance.
  112. // Returns false if *something* is a literal, an item from another store instance,
  113. // or is any object other than an item.
  114. // something:
  115. // Can be anything.
  116. // example:
  117. // | var yes = store.isItem(store.newItem());
  118. // | var no = store.isItem("green");
  119. throw new Error('Unimplemented API: dojo.data.api.Read.isItem');
  120. },
  121. isItemLoaded: function(/* anything */ something){
  122. // summary:
  123. // Returns false if isItem(something) is false. Returns false if
  124. // if isItem(something) is true but the the item is not yet loaded
  125. // in local memory (for example, if the item has not yet been read
  126. // from the server).
  127. // something:
  128. // Can be anything.
  129. // example:
  130. // | var yes = store.isItemLoaded(store.newItem());
  131. // | var no = store.isItemLoaded("green");
  132. throw new Error('Unimplemented API: dojo.data.api.Read.isItemLoaded');
  133. },
  134. loadItem: function(/* object */ keywordArgs){
  135. // summary:
  136. // Given an item, this method loads the item so that a subsequent call
  137. // to store.isItemLoaded(item) will return true. If a call to
  138. // isItemLoaded() returns true before loadItem() is even called,
  139. // then loadItem() need not do any work at all and will not even invoke
  140. // the callback handlers. So, before invoking this method, check that
  141. // the item has not already been loaded.
  142. // keywordArgs:
  143. // An anonymous object that defines the item to load and callbacks to invoke when the
  144. // load has completed. The format of the object is as follows:
  145. // {
  146. // item: object,
  147. // onItem: Function,
  148. // onError: Function,
  149. // scope: object
  150. // }
  151. // The *item* parameter.
  152. // The item parameter is an object that represents the item in question that should be
  153. // contained by the store. This attribute is required.
  154. //
  155. // The *onItem* parameter.
  156. // Function(item)
  157. // The onItem parameter is the callback to invoke when the item has been loaded. It takes only one
  158. // parameter, the fully loaded item.
  159. //
  160. // The *onError* parameter.
  161. // Function(error)
  162. // The onError parameter is the callback to invoke when the item load encountered an error. It takes only one
  163. // parameter, the error object
  164. //
  165. // The *scope* parameter.
  166. // If a scope object is provided, all of the callback functions (onItem,
  167. // onError, etc) will be invoked in the context of the scope object.
  168. // In the body of the callback function, the value of the "this"
  169. // keyword will be the scope object. If no scope object is provided,
  170. // the callback functions will be called in the context of dojo.global().
  171. // For example, onItem.call(scope, item, request) vs.
  172. // onItem.call(dojo.global(), item, request)
  173. if(!this.isItemLoaded(keywordArgs.item)){
  174. throw new Error('Unimplemented API: dojo.data.api.Read.loadItem');
  175. }
  176. },
  177. fetch: function(/* Object */ keywordArgs){
  178. // summary:
  179. // Given a query and set of defined options, such as a start and count of items to return,
  180. // this method executes the query and makes the results available as data items.
  181. // The format and expectations of stores is that they operate in a generally asynchronous
  182. // manner, therefore callbacks are always used to return items located by the fetch parameters.
  183. //
  184. // description:
  185. // A Request object will always be returned and is returned immediately.
  186. // The basic request is nothing more than the keyword args passed to fetch and
  187. // an additional function attached, abort(). The returned request object may then be used
  188. // to cancel a fetch. All data items returns are passed through the callbacks defined in the
  189. // fetch parameters and are not present on the 'request' object.
  190. //
  191. // This does not mean that custom stores can not add methods and properties to the request object
  192. // returned, only that the API does not require it. For more info about the Request API,
  193. // see dojo.data.api.Request
  194. //
  195. // keywordArgs:
  196. // The keywordArgs parameter may either be an instance of
  197. // conforming to dojo.data.api.Request or may be a simple anonymous object
  198. // that may contain any of the following:
  199. // {
  200. // query: query-object or query-string,
  201. // queryOptions: object,
  202. // onBegin: Function,
  203. // onItem: Function,
  204. // onComplete: Function,
  205. // onError: Function,
  206. // scope: object,
  207. // start: int
  208. // count: int
  209. // sort: array
  210. // }
  211. // All implementations should accept keywordArgs objects with any of
  212. // the 9 standard properties: query, onBegin, onItem, onComplete, onError
  213. // scope, sort, start, and count. Some implementations may accept additional
  214. // properties in the keywordArgs object as valid parameters, such as
  215. // {includeOutliers:true}.
  216. //
  217. // The *query* parameter.
  218. // The query may be optional in some data store implementations.
  219. // The dojo.data.api.Read API does not specify the syntax or semantics
  220. // of the query itself -- each different data store implementation
  221. // may have its own notion of what a query should look like.
  222. // However, as of dojo 0.9, 1.0, and 1.1, all the provided datastores in dojo.data
  223. // and dojox.data support an object structure query, where the object is a set of
  224. // name/value parameters such as { attrFoo: valueBar, attrFoo1: valueBar1}. Most of the
  225. // dijit widgets, such as ComboBox assume this to be the case when working with a datastore
  226. // when they dynamically update the query. Therefore, for maximum compatibility with dijit
  227. // widgets the recommended query parameter is a key/value object. That does not mean that the
  228. // the datastore may not take alternative query forms, such as a simple string, a Date, a number,
  229. // or a mix of such. Ultimately, The dojo.data.api.Read API is agnostic about what the query
  230. // format.
  231. // Further note: In general for query objects that accept strings as attribute
  232. // value matches, the store should also support basic filtering capability, such as *
  233. // (match any character) and ? (match single character). An example query that is a query object
  234. // would be like: { attrFoo: "value*"}. Which generally means match all items where they have
  235. // an attribute named attrFoo, with a value that starts with 'value'.
  236. //
  237. // The *queryOptions* parameter
  238. // The queryOptions parameter is an optional parameter used to specify optiosn that may modify
  239. // the query in some fashion, such as doing a case insensitive search, or doing a deep search
  240. // where all items in a hierarchical representation of data are scanned instead of just the root
  241. // items. It currently defines two options that all datastores should attempt to honor if possible:
  242. // {
  243. // ignoreCase: boolean, //Whether or not the query should match case sensitively or not. Default behaviour is false.
  244. // deep: boolean //Whether or not a fetch should do a deep search of items and all child
  245. // //items instead of just root-level items in a datastore. Default is false.
  246. // }
  247. //
  248. // The *onBegin* parameter.
  249. // function(size, request);
  250. // If an onBegin callback function is provided, the callback function
  251. // will be called just once, before the first onItem callback is called.
  252. // The onBegin callback function will be passed two arguments, the
  253. // the total number of items identified and the Request object. If the total number is
  254. // unknown, then size will be -1. Note that size is not necessarily the size of the
  255. // collection of items returned from the query, as the request may have specified to return only a
  256. // subset of the total set of items through the use of the start and count parameters.
  257. //
  258. // The *onItem* parameter.
  259. // function(item, request);
  260. // If an onItem callback function is provided, the callback function
  261. // will be called as each item in the result is received. The callback
  262. // function will be passed two arguments: the item itself, and the
  263. // Request object.
  264. //
  265. // The *onComplete* parameter.
  266. // function(items, request);
  267. //
  268. // If an onComplete callback function is provided, the callback function
  269. // will be called just once, after the last onItem callback is called.
  270. // Note that if the onItem callback is not present, then onComplete will be passed
  271. // an array containing all items which matched the query and the request object.
  272. // If the onItem callback is present, then onComplete is called as:
  273. // onComplete(null, request).
  274. //
  275. // The *onError* parameter.
  276. // function(errorData, request);
  277. // If an onError callback function is provided, the callback function
  278. // will be called if there is any sort of error while attempting to
  279. // execute the query.
  280. // The onError callback function will be passed two arguments:
  281. // an Error object and the Request object.
  282. //
  283. // The *scope* parameter.
  284. // If a scope object is provided, all of the callback functions (onItem,
  285. // onComplete, onError, etc) will be invoked in the context of the scope
  286. // object. In the body of the callback function, the value of the "this"
  287. // keyword will be the scope object. If no scope object is provided,
  288. // the callback functions will be called in the context of dojo.global().
  289. // For example, onItem.call(scope, item, request) vs.
  290. // onItem.call(dojo.global(), item, request)
  291. //
  292. // The *start* parameter.
  293. // If a start parameter is specified, this is a indication to the datastore to
  294. // only start returning items once the start number of items have been located and
  295. // skipped. When this parameter is paired with 'count', the store should be able
  296. // to page across queries with millions of hits by only returning subsets of the
  297. // hits for each query
  298. //
  299. // The *count* parameter.
  300. // If a count parameter is specified, this is a indication to the datastore to
  301. // only return up to that many items. This allows a fetch call that may have
  302. // millions of item matches to be paired down to something reasonable.
  303. //
  304. // The *sort* parameter.
  305. // If a sort parameter is specified, this is a indication to the datastore to
  306. // sort the items in some manner before returning the items. The array is an array of
  307. // javascript objects that must conform to the following format to be applied to the
  308. // fetching of items:
  309. // {
  310. // attribute: attribute || attribute-name-string,
  311. // descending: true|false; // Optional. Default is false.
  312. // }
  313. // Note that when comparing attributes, if an item contains no value for the attribute
  314. // (undefined), then it the default ascending sort logic should push it to the bottom
  315. // of the list. In the descending order case, it such items should appear at the top of the list.
  316. //
  317. // returns:
  318. // The fetch() method will return a javascript object conforming to the API
  319. // defined in dojo.data.api.Request. In general, it will be the keywordArgs
  320. // object returned with the required functions in Request.js attached.
  321. // Its general purpose is to provide a convenient way for a caller to abort an
  322. // ongoing fetch.
  323. //
  324. // The Request object may also have additional properties when it is returned
  325. // such as request.store property, which is a pointer to the datastore object that
  326. // fetch() is a method of.
  327. //
  328. // exceptions:
  329. // Throws an exception if the query is not valid, or if the query
  330. // is required but was not supplied.
  331. //
  332. // example:
  333. // Fetch all books identified by the query and call 'showBooks' when complete
  334. // | var request = store.fetch({query:"all books", onComplete: showBooks});
  335. // example:
  336. // Fetch all items in the story and call 'showEverything' when complete.
  337. // | var request = store.fetch(onComplete: showEverything);
  338. // example:
  339. // Fetch only 10 books that match the query 'all books', starting at the fifth book found during the search.
  340. // This demonstrates how paging can be done for specific queries.
  341. // | var request = store.fetch({query:"all books", start: 4, count: 10, onComplete: showBooks});
  342. // example:
  343. // Fetch all items that match the query, calling 'callback' each time an item is located.
  344. // | var request = store.fetch({query:"foo/bar", onItem:callback});
  345. // example:
  346. // Fetch the first 100 books by author King, call showKing when up to 100 items have been located.
  347. // | var request = store.fetch({query:{author:"King"}, start: 0, count:100, onComplete: showKing});
  348. // example:
  349. // Locate the books written by Author King, sort it on title and publisher, then return the first 100 items from the sorted items.
  350. // | var request = store.fetch({query:{author:"King"}, sort: [{ attribute: "title", descending: true}, {attribute: "publisher"}], ,start: 0, count:100, onComplete: 'showKing'});
  351. // example:
  352. // Fetch the first 100 books by authors starting with the name King, then call showKing when up to 100 items have been located.
  353. // | var request = store.fetch({query:{author:"King*"}, start: 0, count:100, onComplete: showKing});
  354. // example:
  355. // Fetch the first 100 books by authors ending with 'ing', but only have one character before it (King, Bing, Ling, Sing, etc.), then call showBooks when up to 100 items have been located.
  356. // | var request = store.fetch({query:{author:"?ing"}, start: 0, count:100, onComplete: showBooks});
  357. // example:
  358. // Fetch the first 100 books by author King, where the name may appear as King, king, KING, kInG, and so on, then call showKing when up to 100 items have been located.
  359. // | var request = store.fetch({query:{author:"King"}, queryOptions:(ignoreCase: true}, start: 0, count:100, onComplete: showKing});
  360. // example:
  361. // Paging
  362. // | var store = new dojo.data.LargeRdbmsStore({url:"jdbc:odbc:foobar"});
  363. // | var fetchArgs = {
  364. // | query: {type:"employees", name:"Hillary *"}, // string matching
  365. // | sort: [{attribute:"department", descending:true}],
  366. // | start: 0,
  367. // | count: 20,
  368. // | scope: displayer,
  369. // | onBegin: showThrobber,
  370. // | onItem: displayItem,
  371. // | onComplete: stopThrobber,
  372. // | onError: handleFetchError,
  373. // | };
  374. // | store.fetch(fetchArgs);
  375. // | ...
  376. // and then when the user presses the "Next Page" button...
  377. // | fetchArgs.start += 20;
  378. // | store.fetch(fetchArgs); // get the next 20 items
  379. throw new Error('Unimplemented API: dojo.data.api.Read.fetch');
  380. },
  381. getFeatures: function(){
  382. // summary:
  383. // The getFeatures() method returns an simple keyword values object
  384. // that specifies what interface features the datastore implements.
  385. // A simple CsvStore may be read-only, and the only feature it
  386. // implements will be the 'dojo.data.api.Read' interface, so the
  387. // getFeatures() method will return an object like this one:
  388. // {'dojo.data.api.Read': true}.
  389. // A more sophisticated datastore might implement a variety of
  390. // interface features, like 'dojo.data.api.Read', 'dojo.data.api.Write',
  391. // 'dojo.data.api.Identity', and 'dojo.data.api.Attribution'.
  392. return {
  393. 'dojo.data.api.Read': true
  394. };
  395. },
  396. close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
  397. // summary:
  398. // The close() method is intended for instructing the store to 'close' out
  399. // any information associated with a particular request.
  400. //
  401. // description:
  402. // The close() method is intended for instructing the store to 'close' out
  403. // any information associated with a particular request. In general, this API
  404. // expects to recieve as a parameter a request object returned from a fetch.
  405. // It will then close out anything associated with that request, such as
  406. // clearing any internal datastore caches and closing any 'open' connections.
  407. // For some store implementations, this call may be a no-op.
  408. //
  409. // request:
  410. // An instance of a request for the store to use to identify what to close out.
  411. // If no request is passed, then the store should clear all internal caches (if any)
  412. // and close out all 'open' connections. It does not render the store unusable from
  413. // there on, it merely cleans out any current data and resets the store to initial
  414. // state.
  415. //
  416. // example:
  417. // | var request = store.fetch({onComplete: doSomething});
  418. // | ...
  419. // | store.close(request);
  420. throw new Error('Unimplemented API: dojo.data.api.Read.close');
  421. },
  422. getLabel: function(/* item */ item){
  423. // summary:
  424. // Method to inspect the item and return a user-readable 'label' for the item
  425. // that provides a general/adequate description of what the item is.
  426. //
  427. // description:
  428. // Method to inspect the item and return a user-readable 'label' for the item
  429. // that provides a general/adequate description of what the item is. In general
  430. // most labels will be a specific attribute value or collection of the attribute
  431. // values that combine to label the item in some manner. For example for an item
  432. // that represents a person it may return the label as: "firstname lastlame" where
  433. // the firstname and lastname are attributes on the item. If the store is unable
  434. // to determine an adequate human readable label, it should return undefined. Users that wish
  435. // to customize how a store instance labels items should replace the getLabel() function on
  436. // their instance of the store, or extend the store and replace the function in
  437. // the extension class.
  438. //
  439. // item:
  440. // The item to return the label for.
  441. //
  442. // returns:
  443. // A user-readable string representing the item or undefined if no user-readable label can
  444. // be generated.
  445. throw new Error('Unimplemented API: dojo.data.api.Read.getLabel');
  446. },
  447. getLabelAttributes: function(/* item */ item){
  448. // summary:
  449. // Method to inspect the item and return an array of what attributes of the item were used
  450. // to generate its label, if any.
  451. //
  452. // description:
  453. // Method to inspect the item and return an array of what attributes of the item were used
  454. // to generate its label, if any. This function is to assist UI developers in knowing what
  455. // attributes can be ignored out of the attributes an item has when displaying it, in cases
  456. // where the UI is using the label as an overall identifer should they wish to hide
  457. // redundant information.
  458. //
  459. // item:
  460. // The item to return the list of label attributes for.
  461. //
  462. // returns:
  463. // An array of attribute names that were used to generate the label, or null if public attributes
  464. // were not used to generate the label.
  465. throw new Error('Unimplemented API: dojo.data.api.Read.getLabelAttributes');
  466. }
  467. });
  468. return dojo.data.api.Read;
  469. });