datasource-arrayschema.js revision 32b6fd78d1787b0afecb1970111f14a46a569aa7
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly/**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * Extends DataSource with schema-parsing on array data.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly *
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @module datasource
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @submodule datasource-arrayschema
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly/**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * Adds schema-parsing to the YUI DataSource utility.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @class DataSourceArraySchema
32b6fd78d1787b0afecb1970111f14a46a569aa7Jenny Han Donnelly * @extends Plugin.Base
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnellyvar DataSourceArraySchema = function() {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly DataSourceArraySchema.superclass.constructor.apply(this, arguments);
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly};
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han DonnellyY.mix(DataSourceArraySchema, {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * The namespace for the plugin. This will be the property on the host which
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * references the plugin instance.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly *
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @property NS
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @type String
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @static
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @final
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @value "schema"
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly NS: "schema",
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * Class name.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly *
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @property NAME
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @type String
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @static
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @final
1e0aa6df8881a856ec262f103bfee1c0a05d19b9Jenny Han Donnelly * @value "dataSourceArraySchema"
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
1e0aa6df8881a856ec262f103bfee1c0a05d19b9Jenny Han Donnelly NAME: "dataSourceArraySchema",
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly //
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly // DataSourceArraySchema Attributes
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly //
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly ATTRS: {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly schema: {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly //value: {}
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly }
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly }
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly});
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han DonnellyY.extend(DataSourceArraySchema, Y.Plugin.Base, {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * Internal init() handler.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly *
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @method initializer
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @param config {Object} Config object.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @private
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly initializer: function(config) {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly this.doBefore("_defDataFn", this._beforeDefDataFn);
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly },
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly /**
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * Parses raw data into a normalized response.
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly *
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @method _beforeDefDataFn
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dl>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>request (Object)</dt> <dd>The request.</dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dl>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>success (Function)</dt> <dd>Success handler.</dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * </dl>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * </dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * <dt>data (Object)</dt> <dd>Raw data.</dd>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * </dl>
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly * @protected
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly */
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly _beforeDefDataFn: function(e) {
350839d3f8c71a522972265f77a8b5a3fbf6c82fJenny Han Donnelly var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data,
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly response = Y.DataSchema.Array.apply(this.get("schema"), data);
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly // Default
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly if(!response) {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly response = {
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly meta: {},
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly results: data
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly };
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly }
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly this.get("host").fire("response", Y.mix({response:response}, e));
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn");
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly }
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly});
fe0eaf3c7c1942b0a300cd5f3952ada16fb18781Jenny Han Donnelly
21a3d9293597d65e84324fe57b7158e3d67a46c1Jenny Han DonnellyY.namespace('Plugin').DataSourceArraySchema = DataSourceArraySchema;