datasource-jsonparser-debug.js revision 404905c7e219f117d5a09626421a02d395bb110e
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke SmithYUI.add('datasource-jsonparser', function(Y) {
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith/**
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * Extends DataSource with schema-based JSON parsing functionality.
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith *
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @module datasource
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @submodule datasource-dataparser
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith */
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith/**
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * Adds parsability to the YUI DataSource utility.
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @class DataSourceJSONParser
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @extends Plugin
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith */
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smithvar DataSourceJSONParser = function() {
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith DataSourceJSONParser.superclass.constructor.apply(this, arguments);
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith};
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke SmithY.mix(DataSourceJSONParser, {
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith /**
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * The namespace for the plugin. This will be the property on the host which
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * references the plugin instance.
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith *
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @property NS
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @type String
eb6c1c09177446c3a7fa974e4658cbd555c5be18Luke Smith * @static
a3b15d60042c81a524cebb94370e5a234a19d04bLuke Smith * @final
a3b15d60042c81a524cebb94370e5a234a19d04bLuke Smith * @value "parser"
a3b15d60042c81a524cebb94370e5a234a19d04bLuke Smith */
a3b15d60042c81a524cebb94370e5a234a19d04bLuke Smith NS: "parser",
a3b15d60042c81a524cebb94370e5a234a19d04bLuke Smith
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "DataSourceJSONParser"
*/
NAME: "DataSourceJSONParser",
/////////////////////////////////////////////////////////////////////////////
//
// DataSourceCache Attributes
//
/////////////////////////////////////////////////////////////////////////////
ATTRS: {
parser: {
readOnly: true,
value: Y.DataParser.JSON,
useRef: true
},
schema: {
//value: {}
}
}
});
Y.extend(DataSourceJSONParser, Y.Plugin, {
/**
* 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 response = (this.get("parser").parse(this.get("schema"), e.data));
if(!response) {
response = {
meta: {},
results: e.data
};
}
this._owner.fire("response", Y.mix({response:response}, e));
return new Y.Do.Halt("DataSourceJSONParser plugin halted _defDataFn");
}
});
Y.namespace('plugin');
Y.plugin.DataSourceJSONParser = DataSourceJSONParser;
}, '@VERSION@' ,{requires:['plugin', 'datasource-base', 'dataparser-json']});