PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/datasource/docs/index.mustache

https://github.com/vrennert/yui3
Mustache | 306 lines | 282 code | 24 blank | 0 comment | 8 complexity | 13f56fc40ff964797ef19e292858b28a MD5 | raw file
  1. <div class="intro component">
  2. <p>
  3. The DataSource Utility provides a consistent API for the retrieval of
  4. data from arbitrary sources over a variety of supported protocols.
  5. DataSource plugins and extensions enable additional functionality such
  6. as schema normalization, caching, and polling of data.
  7. </p>
  8. </div>
  9. {{>getting-started}}
  10. <h2 id="using">Using DataSources</h2>
  11. <h3 id="basics">DataSource basics</h3>
  12. <p>
  13. The DataSource Utility uses a callback mechanism to manage the data
  14. retrieval process across a wide variety of potential sources. Define your
  15. callback object with custom functions that will execute when the data
  16. returns from your source. The <code>sendRequest()</code> method accepts an
  17. object literal with properties for the request value, a callback object,
  18. and/or any configuration values for the request.
  19. </p>
  20. ```
  21. myDataSource.sendRequest({
  22. request: myRequest,
  23. on: {
  24. success: function(e){
  25. alert(e.response);
  26. },
  27. failure: function(e){
  28. alert(e.error.message);
  29. }
  30. }
  31. });
  32. ```
  33. <p>
  34. You must instantiate the appropriate DataSource subclass for your source of
  35. data.
  36. </p>
  37. <h4 id="local">Local sources</h4>
  38. <p>
  39. Use DataSource.Local when you are working with data that is held in local
  40. memory, such as a JavaScript array or object.
  41. </p>
  42. ```
  43. var myDataSource = new Y.DataSource.Local({source:["a", "b", "c"]});
  44. ```
  45. <h4 id="get">Remote sources with the Get Utility</h4>
  46. <p>
  47. Use DataSource.Get to access data coming from a server via the Get Utility.
  48. The Get Utility supports data retrieval from cross-domain resources without
  49. the need for a proxy, but the server must return JSON data and support a
  50. script callback parameter in order for the response to return properly.
  51. This parameter specifies the name of the internally defined function that
  52. the return data will be wrapped in when it returns to the page.
  53. </p>
  54. ```
  55. var myDataSource = new Y.DataSource.Get({
  56. source: "http://query.yahooapis.com/v1/public/yql?format=json&"
  57. });
  58. ```
  59. <p>
  60. You should not modify the internally assigned value of this script callback
  61. parameter. However, you may need to set the parameter name to a different
  62. value so that your server will accept it. By default, the script callback
  63. parameter name is <code>"callback"</code>, but this value can be changed
  64. via the Attribute <code>scriptCallbackParam</code>.
  65. </p>
  66. ```
  67. // By default the request is sent to
  68. // "http://query.yahooapis.com/v1/public/yql?format=json&q=foo&callback=YUI.Env.DataSource.callbacks[0]"
  69. myDataSource.sendRequest({
  70. request: "q=foo",
  71. callback: myCallback
  72. });
  73. // But the parameter name can be customized to match the server requirement
  74. myDataSource.set("scriptCallbackParam", "cbFunc");
  75. // So now the request is sent to
  76. // "http://query.yahooapis.com/v1/public/yql?format=json&q=foo&cbFunc=YUI.Env.DataSource.callbacks[0]"
  77. myDataSource.sendRequest({
  78. request: "q=foo",
  79. callback: myCallback
  80. });
  81. ```
  82. <p>
  83. Use the DataSourceJSONSchema plugin to normalize the data that is sent to
  84. your callack.
  85. </p>
  86. ```
  87. // Normalize the data sent to myCallback
  88. myDataSource.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
  89. schema: {
  90. resultListLocator: "myResults",
  91. resultFields: ["myField1", "myField2"]
  92. }
  93. }});
  94. ```
  95. <h4 id="io">Remote sources with the IO Utility</h4>
  96. <p>
  97. DataSource.IO is used to access data coming from a server via the IO
  98. Utility. Note that accessing a cross-domain server will require a
  99. same-domain proxy or enabling IO's XDR feature, in order to bypass standard
  100. browser security restrictions.
  101. </p>
  102. ```
  103. var myDataSource = new Y.DataSource.IO({source:"./myScript.php"});
  104. ```
  105. <p>
  106. The IO Utility supports retrieval of multiple data formats, including JSON,
  107. XML, and plain text. Use the appropriate DataSchema plugin to normalize the
  108. data that is sent to your callback.
  109. </p>
  110. ```
  111. myDataSource.plug({fn: Y.Plugin.DataSourceXMLSchema, cfg: {
  112. schema: {
  113. resultListLocator: "resultNodeName",
  114. resultFields: [{key:"myField1", locator:"xpath/to/value"}]
  115. }
  116. }});
  117. ```
  118. <h4 id="function">Sources using custom functions</h4>
  119. <p>
  120. Defining your own JavaScript function that returns data for a given request
  121. allows full customization of the data retrieval mechanism.
  122. </p>
  123. ```
  124. var myDataSource = new Y.DataSource.Function({
  125. source: function (request) {
  126. return data;
  127. }
  128. });
  129. ```
  130. <p>
  131. Since your data can return data of any format, you may consider ways to
  132. taking advantage of the built-in infrastructure, including using a
  133. DataSchema plugin to normalize the data that is sent to your callback.
  134. </p>
  135. ```
  136. var myDataSource = new Y.DataSource.Function({
  137. source: function (request) {
  138. return [["ann", 123], ["bill", 456]];
  139. }
  140. });
  141. myDataSource.plug({fn: Y.Plugin.DataSourceArraySchema, cfg: {
  142. schema: {
  143. resultFields: ["name","id"]
  144. }
  145. }});
  146. ```
  147. <h3 id="caching">Caching</h3>
  148. <p>
  149. The DataSourceCache plugin provides integrated caching functionality to
  150. your DataSource instance. Use the DataSource's <code>plug()</code> method
  151. to instantiate a Cache instance. Set the <code>max</code> Attribute value
  152. to the maximum number of entries the Cache should hold.
  153. </p>
  154. ```
  155. myDataSource.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
  156. ```
  157. <p>
  158. Once the plugin is enabled, it will handle caching and retrieval of values
  159. seamlessly for you without the need for extra code. However, all the
  160. methods and properties of the Cache instance is available on the DataSource
  161. instance's <code>cache</code> namepace.
  162. </p>
  163. ```
  164. // Flush myDataSource's cache.
  165. myDataSource.cache.flush();
  166. // Disable myDataSource's cache
  167. myDataSource.cache.set("max", 0);
  168. ```
  169. <h3 id="polling">Polling</h3>
  170. <p>
  171. Pollable is a DataSource extension that enhances the class with polling
  172. functionality. Once the extension is applied, all instances of DataSource
  173. will have available on their prototype the methods that enable and disable
  174. requests sent at regular intervals. To apply the extension, simply include
  175. the <code>datasource-polling</code> sub-module in your
  176. <code>YUI.use()</code> statement.
  177. </p>
  178. ```
  179. YUI().use('datasource-io', 'datasource-polling', 'json-parse', function(Y) {
  180. var onlineFriends = Y.one('#friend-count'),
  181. friendData,
  182. intervalId;
  183. friendData = new Y.DataSource.IO({
  184. source: '/services/friends/'
  185. });
  186. // Start polling the server every 10 seconds
  187. intervalId = friendData.setInterval(10000, {
  188. request : Y.one('#user-id').get('value'),
  189. callback: {
  190. success: function (e) {
  191. var friends = Y.JSON.parse(e.response.results[0]).friendCount;
  192. if (!friends) {
  193. friends = 'No friends. You should go outside more.';
  194. }
  195. onlineFriends.set('text', friends);
  196. },
  197. failure: function (e) {
  198. onlineFriends.set('text',
  199. '(Bang) Ouch! ' + e.error.message + ' happened!');
  200. // Stop polling
  201. friendData.clearInterval(intervalId);
  202. }
  203. }
  204. });
  205. });
  206. ```
  207. <h3 id="events">Events</h3>
  208. <table>
  209. <thead>
  210. <tr>
  211. <th>Event</th>
  212. <th>When</th>
  213. <th>Event properties</th>
  214. </tr>
  215. </thead>
  216. <tbody>
  217. <tr>
  218. <td><code>request</code></td>
  219. <td>Request is made.</td>
  220. <td>
  221. <dl>
  222. <dt><code>tId</code></dt>
  223. <dd>Unique transaction ID.</dd>
  224. <dt><code>request</code></dt>
  225. <dd>The request value.</dd>
  226. <dt><code>callback</code></dt>
  227. <dd>The callback object.</dd>
  228. <dt><code>cfg</code></dt>
  229. <dd>The configuration object.</dd>
  230. </dl>
  231. </td>
  232. </tr>
  233. <tr>
  234. <td><code>data</code></td>
  235. <td>Raw data is received from the source.</td>
  236. <td>
  237. All properties from `request` plus
  238. <dl>
  239. <dt><code>data</code></dt>
  240. <dd>The raw data.</dd>
  241. </dl>
  242. </td>
  243. </tr>
  244. <tr>
  245. <td><code>response</code></td>
  246. <td>Response is returned to a callback function.</td>
  247. <td>
  248. All properties from `data` plus
  249. <dl>
  250. <dt><code>response</code></dt>
  251. <dd>Data normalized into a response object.</dd>
  252. </dl>
  253. </td>
  254. </tr>
  255. <tr>
  256. <td><code>error</code></td>
  257. <td>After `response` event, before the configured failure callback is executed.</td>
  258. <td>Same properties as the `response` event</td>
  259. </tr>
  260. </tbody>
  261. </table>