datatable-datasource.js revision 820d7f2eab2a78412c9803335bb10a2974e7fbf5
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric FerraiuoloYUI.add('datatable-datasource', function(Y) {
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/**
feb1dc670b0d4e1c6fcff58b35b36da3dfc5f18cJenny Donnelly * Plugs DataTable with DataSource integration.
feb1dc670b0d4e1c6fcff58b35b36da3dfc5f18cJenny Donnelly *
feb1dc670b0d4e1c6fcff58b35b36da3dfc5f18cJenny Donnelly * @module datatable
feb1dc670b0d4e1c6fcff58b35b36da3dfc5f18cJenny Donnelly * @submodule datatable-datasource
feb1dc670b0d4e1c6fcff58b35b36da3dfc5f18cJenny Donnelly */
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/**
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * Adds DataSource integration to DataTable.
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @class DataTableDataSource
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @extends Plugin.Base
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo */
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolofunction DataTableDataSource() {
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo DataTableDataSource.superclass.constructor.apply(this, arguments);
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo}
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/////////////////////////////////////////////////////////////////////////////
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo//
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo// STATIC PROPERTIES
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo//
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/////////////////////////////////////////////////////////////////////////////
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric FerraiuoloY.mix(DataTableDataSource, {
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo /**
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * The namespace for the plugin. This will be the property on the host which
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * references the plugin instance.
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo *
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @property NS
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @type String
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @static
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @final
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @value "datasource"
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo */
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo NS: "datasource",
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo /**
23905ae5a911176394ebe0bd96cd4a443d9929ceEric Ferraiuolo * Class name.
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo *
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @property NAME
23905ae5a911176394ebe0bd96cd4a443d9929ceEric Ferraiuolo * @type String
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @static
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @final
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @value "dataTableDataSource"
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo */
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo NAME: "dataTableDataSource",
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/////////////////////////////////////////////////////////////////////////////
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo//
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo// ATTRIBUTES
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo//
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo/////////////////////////////////////////////////////////////////////////////
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo ATTRS: {
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo /**
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @attribute datasource
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @description Pointer to DataSource instance.
1b7b07a780dc36179ffd6c2ba65e258b8dba96c5Eric Ferraiuolo * @type {DataSource}
*/
datasource: {
setter: "_setDataSource"
},
/**
* @attribute initialRequest
* @description Request sent to DataSource immediately upon initialization.
* @type Object
*/
initialRequest: {
setter: "_setInitialRequest"
}
}
});
/////////////////////////////////////////////////////////////////////////////
//
// PROTOTYPE
//
/////////////////////////////////////////////////////////////////////////////
Y.extend(DataTableDataSource, Y.Plugin.Base, {
/////////////////////////////////////////////////////////////////////////////
//
// ATTRIBUTE HELPERS
//
/////////////////////////////////////////////////////////////////////////////
/**
* @method _setDataSource
* @description Creates new DataSource instance if one is not provided.
* @param ds {Object | Y.DataSource}
* @return {DataSource}
* @private
*/
_setDataSource: function(ds) {
return ds || new Y.DataSource.Local(ds);
},
/**
* @method _setInitialRequest
* @description Sends request to DataSource.
* @param request {Object} DataSource request.
* @private
*/
_setInitialRequest: function(request) {
},
/////////////////////////////////////////////////////////////////////////////
//
// METHODS
//
/////////////////////////////////////////////////////////////////////////////
/**
* Initializer.
*
* @method initializer
* @param config {Object} Config object.
* @private
*/
initializer: function(config) {
if(!Y.Lang.isUndefined(config.initialRequest)) {
this.load({request:config.initialRequest});
}
},
////////////////////////////////////////////////////////////////////////////
//
// DATA
//
////////////////////////////////////////////////////////////////////////////
/**
* Load data by calling DataSource's sendRequest() method under the hood.
*
* @method load
* @param config {object} Optional configuration parameters:
*
* <dl>
* <dt>request</dt><dd>Pass in a new request, or initialRequest is used.</dd>
* <dt>callback</dt><dd>Pass in DataSource callback object, or the following default is used:
* <dl>
* <dt>success</dt><dd>datatable.onDataReturnInitializeTable</dd>
* <dt>failure</dt><dd>datatable.onDataReturnInitializeTable</dd>
* <dt>scope</dt><dd>datatable</dd>
* <dt>argument</dt><dd>datatable.getState()</dd>
* </dl>
* </dd>
* <dt>datasource</dt><dd>Pass in a new DataSource instance to override the current DataSource for this transaction.</dd>
* </dl>
*/
load: function(config) {
config = config || {};
config.request = config.request || this.get("initialRequest");
config.callback = config.callback || {
success: Y.bind(this.onDataReturnInitializeTable, this),
failure: Y.bind(this.onDataReturnInitializeTable, this),
argument: this.get("host").get("state") //TODO
};
var ds = (config.datasource || this.get("datasource"));
if(ds) {
ds.sendRequest(config);
}
},
/**
* Callback function passed to DataSource's sendRequest() method populates
* an entire DataTable with new data, clearing previous data, if any.
*
* @method onDataReturnInitializeTable
* @param e {Event.Facade} DataSource Event Facade object.
*/
onDataReturnInitializeTable : function(e) {
var records = (e.response && e.response.results) || [];
this.get("host").set("data", records);
}
});
Y.namespace("Plugin").DataTableDataSource = DataTableDataSource;
}, '@VERSION@' ,{requires:['datatable-base','plugin','datasource-local']});