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