API Docs for: 3.10.3
Show:

File: datatable/js/datatable-datasource.js

  1. /**
  2. * Plugs DataTable with DataSource integration.
  3. *
  4. * @module datatable
  5. * @submodule datatable-datasource
  6. */
  7.  
  8. /**
  9. * Adds DataSource integration to DataTable.
  10. * @class Plugin.DataTableDataSource
  11. * @extends Plugin.Base
  12. */
  13. function DataTableDataSource() {
  14. DataTableDataSource.superclass.constructor.apply(this, arguments);
  15. }
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. //
  19. // STATIC PROPERTIES
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22. Y.mix(DataTableDataSource, {
  23. /**
  24. * The namespace for the plugin. This will be the property on the host which
  25. * references the plugin instance.
  26. *
  27. * @property NS
  28. * @type String
  29. * @static
  30. * @final
  31. * @value "datasource"
  32. */
  33. NS: "datasource",
  34.  
  35. /**
  36. * Class name.
  37. *
  38. * @property NAME
  39. * @type String
  40. * @static
  41. * @final
  42. * @value "dataTableDataSource"
  43. */
  44. NAME: "dataTableDataSource",
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. //
  48. // ATTRIBUTES
  49. //
  50. /////////////////////////////////////////////////////////////////////////////
  51. ATTRS: {
  52. /**
  53. * @attribute datasource
  54. * @description Pointer to DataSource instance.
  55. * @type {DataSource}
  56. */
  57. datasource: {
  58. setter: "_setDataSource"
  59. },
  60.  
  61. /**
  62. * @attribute initialRequest
  63. * @description Request sent to DataSource immediately upon initialization.
  64. * @type Object
  65. */
  66. initialRequest: {
  67. setter: "_setInitialRequest"
  68. }
  69. }
  70. });
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. //
  74. // PROTOTYPE
  75. //
  76. /////////////////////////////////////////////////////////////////////////////
  77. Y.extend(DataTableDataSource, Y.Plugin.Base, {
  78. /////////////////////////////////////////////////////////////////////////////
  79. //
  80. // ATTRIBUTE HELPERS
  81. //
  82. /////////////////////////////////////////////////////////////////////////////
  83. /**
  84. * @method _setDataSource
  85. * @description Creates new DataSource instance if one is not provided.
  86. * @param ds {Object | Y.DataSource}
  87. * @return {DataSource}
  88. * @private
  89. */
  90. _setDataSource: function(ds) {
  91. return ds || new Y.DataSource.Local(ds);
  92. },
  93.  
  94. /**
  95. * @method _setInitialRequest
  96. * @description Sends request to DataSource.
  97. * @param request {Object} DataSource request.
  98. * @private
  99. */
  100. _setInitialRequest: function(/* request */) {
  101. },
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. //
  105. // METHODS
  106. //
  107. /////////////////////////////////////////////////////////////////////////////
  108. /**
  109. * Initializer.
  110. *
  111. * @method initializer
  112. * @param config {Object} Config object.
  113. * @private
  114. */
  115. initializer: function(config) {
  116. if(!Y.Lang.isUndefined(config.initialRequest)) {
  117. this.load({request:config.initialRequest});
  118. }
  119. },
  120.  
  121. ////////////////////////////////////////////////////////////////////////////
  122. //
  123. // DATA
  124. //
  125. ////////////////////////////////////////////////////////////////////////////
  126.  
  127. /**
  128. * Load data by calling DataSource's sendRequest() method under the hood.
  129. *
  130. * @method load
  131. * @param config {object} Optional configuration parameters:
  132. *
  133. * <dl>
  134. * <dt>request</dt><dd>Pass in a new request, or initialRequest is used.</dd>
  135. * <dt>callback</dt><dd>Pass in DataSource callback object, or the following default is used:
  136. * <dl>
  137. * <dt>success</dt><dd>datatable.onDataReturnInitializeTable</dd>
  138. * <dt>failure</dt><dd>datatable.onDataReturnInitializeTable</dd>
  139. * <dt>scope</dt><dd>datatable</dd>
  140. * <dt>argument</dt><dd>datatable.getState()</dd>
  141. * </dl>
  142. * </dd>
  143. * <dt>datasource</dt><dd>Pass in a new DataSource instance to override the current DataSource for this transaction.</dd>
  144. * </dl>
  145. */
  146. load: function(config) {
  147. config = config || {};
  148. config.request = config.request || this.get("initialRequest");
  149. config.callback = config.callback || {
  150. success: Y.bind(this.onDataReturnInitializeTable, this),
  151. failure: Y.bind(this.onDataReturnInitializeTable, this),
  152. argument: this.get("host").get("state") //TODO
  153. };
  154.  
  155. var ds = (config.datasource || this.get("datasource"));
  156. if(ds) {
  157. ds.sendRequest(config);
  158. }
  159. },
  160.  
  161. /**
  162. * Callback function passed to DataSource's sendRequest() method populates
  163. * an entire DataTable with new data, clearing previous data, if any.
  164. *
  165. * @method onDataReturnInitializeTable
  166. * @param e {Event.Facade} DataSource Event Facade object.
  167. */
  168. onDataReturnInitializeTable : function(e) {
  169. var records = (e.response && e.response.results) || [];
  170.  
  171. this.get("host").set("data", records);
  172. }
  173. });
  174.  
  175. Y.namespace("Plugin").DataTableDataSource = DataTableDataSource;
  176.