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