3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('datatable-datasource', function(Y) {
10 * Plugs DataTable with DataSource integration.
13 * @submodule datatable-datasource
17 * Adds DataSource integration to DataTable.
18 * @class Plugin.DataTableDataSource
19 * @extends Plugin.Base
21 function DataTableDataSource() {
22 DataTableDataSource.superclass.constructor.apply(this, arguments);
25 /////////////////////////////////////////////////////////////////////////////
29 /////////////////////////////////////////////////////////////////////////////
30 Y.mix(DataTableDataSource, {
32 * The namespace for the plugin. This will be the property on the host which
33 * references the plugin instance.
50 * @value "dataTableDataSource"
52 NAME: "dataTableDataSource",
54 /////////////////////////////////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////////////////
61 * @attribute datasource
62 * @description Pointer to DataSource instance.
66 setter: "_setDataSource"
70 * @attribute initialRequest
71 * @description Request sent to DataSource immediately upon initialization.
75 setter: "_setInitialRequest"
80 /////////////////////////////////////////////////////////////////////////////
84 /////////////////////////////////////////////////////////////////////////////
85 Y.extend(DataTableDataSource, Y.Plugin.Base, {
86 /////////////////////////////////////////////////////////////////////////////
90 /////////////////////////////////////////////////////////////////////////////
92 * @method _setDataSource
93 * @description Creates new DataSource instance if one is not provided.
94 * @param ds {Object | Y.DataSource}
95 * @return {DataSource}
98 _setDataSource: function(ds) {
99 return ds || new Y.DataSource.Local(ds);
103 * @method _setInitialRequest
104 * @description Sends request to DataSource.
105 * @param request {Object} DataSource request.
108 _setInitialRequest: function(request) {
111 /////////////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////////////
119 * @method initializer
120 * @param config {Object} Config object.
123 initializer: function(config) {
124 if(!Y.Lang.isUndefined(config.initialRequest)) {
125 this.load({request:config.initialRequest});
129 ////////////////////////////////////////////////////////////////////////////
133 ////////////////////////////////////////////////////////////////////////////
136 * Load data by calling DataSource's sendRequest() method under the hood.
139 * @param config {object} Optional configuration parameters:
142 * <dt>request</dt><dd>Pass in a new request, or initialRequest is used.</dd>
143 * <dt>callback</dt><dd>Pass in DataSource callback object, or the following default is used:
145 * <dt>success</dt><dd>datatable.onDataReturnInitializeTable</dd>
146 * <dt>failure</dt><dd>datatable.onDataReturnInitializeTable</dd>
147 * <dt>scope</dt><dd>datatable</dd>
148 * <dt>argument</dt><dd>datatable.getState()</dd>
151 * <dt>datasource</dt><dd>Pass in a new DataSource instance to override the current DataSource for this transaction.</dd>
154 load: function(config) {
155 config = config || {};
156 config.request = config.request || this.get("initialRequest");
157 config.callback = config.callback || {
158 success: Y.bind(this.onDataReturnInitializeTable, this),
159 failure: Y.bind(this.onDataReturnInitializeTable, this),
160 argument: this.get("host").get("state") //TODO
163 var ds = (config.datasource || this.get("datasource"));
165 ds.sendRequest(config);
170 * Callback function passed to DataSource's sendRequest() method populates
171 * an entire DataTable with new data, clearing previous data, if any.
173 * @method onDataReturnInitializeTable
174 * @param e {Event.Facade} DataSource Event Facade object.
176 onDataReturnInitializeTable : function(e) {
177 var records = (e.response && e.response.results) || [];
179 this.get("host").set("data", records);
183 Y.namespace("Plugin").DataTableDataSource = DataTableDataSource;
186 }, '3.5.1' ,{requires:['datatable-base','plugin','datasource-local']});