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