datasource-arrayschema.js revision 2634b0e23224a943745ebee1c4b016a207ab0564
0N/AYUI.add('datasource-arrayschema', function(Y) {
0N/A
0N/A/**
0N/A * Extends DataSource with schema-parsing on array data.
0N/A *
0N/A * @module datasource
0N/A * @submodule datasource-arrayschema
0N/A */
0N/A
0N/A/**
0N/A * Adds schema-parsing to the YUI DataSource utility.
0N/A * @class DataSourceArraySchema
0N/A * @extends Plugin
0N/A */
0N/Avar DataSourceArraySchema = function() {
0N/A DataSourceArraySchema.superclass.constructor.apply(this, arguments);
0N/A};
0N/A
0N/AY.mix(DataSourceArraySchema, {
0N/A /**
1416N/A * The namespace for the plugin. This will be the property on the host which
0N/A * references the plugin instance.
1056N/A *
686N/A * @property NS
686N/A * @type String
816N/A * @static
0N/A * @final
0N/A * @value "schema"
0N/A */
0N/A NS: "schema",
816N/A
0N/A /**
1369N/A * Class name.
1369N/A *
1369N/A * @property NAME
928N/A * @type String
928N/A * @static
1416N/A * @final
928N/A * @value "DataSourceArraySchema"
928N/A */
0N/A NAME: "DataSourceArraySchema",
0N/A
1004N/A /////////////////////////////////////////////////////////////////////////////
0N/A //
0N/A // DataSourceArraySchema Attributes
0N/A //
0N/A /////////////////////////////////////////////////////////////////////////////
0N/A
0N/A ATTRS: {
0N/A schema: {
816N/A //value: {}
1004N/A }
928N/A }
974N/A});
Y.extend(DataSourceArraySchema, Y.Plugin.Base, {
/**
* Internal init() handler.
*
* @method initializer
* @param config {Object} Config object.
* @private
*/
initializer: function(config) {
this.doBefore("_defDataFn", this._beforeDefDataFn);
},
/**
* Parses raw data into a normalized response.
*
* @method _beforeDefDataFn
* <dl>
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
* <dt>request (Object)</dt> <dd>The request.</dd>
* <dt>callback (Object)</dt> <dd>The callback object with the following properties:
* <dl>
* <dt>success (Function)</dt> <dd>Success handler.</dd>
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
* <dt>scope (Object)</dt> <dd>Execution context.</dd>
* </dl>
* </dd>
* <dt>data (Object)</dt> <dd>Raw data.</dd>
* </dl>
* @protected
*/
_beforeDefDataFn: function(e) {
var data = ((this.get("host") instanceof Y.DataSource.XHR) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data,
response = Y.DataSchema.Array.apply(this.get("schema"), data);
// Default
if(!response) {
response = {
meta: {},
results: data
};
}
this.get("host").fire("response", Y.mix({response:response}, e));
return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn");
}
});
Y.namespace('plugin').DataSourceArraySchema = DataSourceArraySchema;
}, '@VERSION@' ,{requires:['plugin', 'datasource-local', 'dataschema-array']});