datasource-function.js revision 65b755664437543e907b9557027a4a608cc84c31
986N/A/**
986N/A * The DataSource utility provides a common configurable interface for widgets to
986N/A * access a variety of data, from JavaScript arrays to online database servers.
986N/A *
986N/A * @module datasource
986N/A */
986N/Avar LANG = Y.Lang,
986N/A/**
986N/A * Function subclass for the YUI DataSource utility.
986N/A * @class DataSource.Function
986N/A * @extends DataSource.Local
986N/A * @constructor
986N/A */
986N/A DSFn = function() {
986N/A DSFn.superclass.constructor.apply(this, arguments);
986N/A };
986N/A
986N/A
986N/A /////////////////////////////////////////////////////////////////////////////
986N/A //
986N/A // DataSource.Function static properties
1185N/A //
1185N/A /////////////////////////////////////////////////////////////////////////////
986N/AY.mix(DSFn, {
986N/A /**
986N/A * Class name.
986N/A *
986N/A * @property NAME
986N/A * @type String
986N/A * @static
986N/A * @final
986N/A * @value "DataSource.Function"
986N/A */
986N/A NAME: "DataSource.Function",
986N/A
986N/A
986N/A /////////////////////////////////////////////////////////////////////////////
1185N/A //
1185N/A // DataSource.Function Attributes
986N/A //
986N/A /////////////////////////////////////////////////////////////////////////////
986N/A
986N/A ATTRS: {
986N/A /**
986N/A * @attribute source
986N/A * @description Pointer to live data.
986N/A * @type MIXED
1185N/A * @default null
1185N/A */
1185N/A source: {
1185N/A validator: LANG.isFunction
986N/A },
986N/A
986N/A /**
1185N/A * Context in which to execute the function. By default, is the DataSource
1185N/A * instance itself. If set, the function will receive the DataSource instance
1185N/A * as an additional argument.
1185N/A *
1185N/A * @property scope
986N/A * @type Object
986N/A * @default null
986N/A */
1185N/A context: {
1185N/A value: null
1185N/A }
1185N/A }
1185N/A});
1185N/A
1185N/AY.extend(DSFn, Y.DataSource.Local, {
986N/A
1185N/A
1185N/A /**
1185N/A * Internal init() handler.
1185N/A *
1185N/A * @method initializer
986N/A * @param config {Object} Config object.
986N/A * @private
986N/A */
1185N/A initializer: function(config) {
1185N/A
1185N/A },
1185N/A
1185N/A /**
1185N/A * Passes query string to IO. Fires <code>response</code> event when
1185N/A * response is received asynchronously.
986N/A *
1185N/A * @method _defRequestFn
1185N/A * @param e {Event.Facade} Event Facade with the following properties:
1185N/A * <dl>
1185N/A * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
1185N/A * <dt>request (Object)</dt> <dd>The request.</dd>
986N/A * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
986N/A * <dl>
986N/A * <dt>success (Function)</dt> <dd>Success handler.</dd>
1185N/A * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
1185N/A * <dt>scope (Object)</dt> <dd>Execution context.</dd>
1185N/A * </dl>
1185N/A * </dd>
1185N/A * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
1185N/A * </dl>
1185N/A * @protected
986N/A */
1185N/A _defRequestFn: function(e) {
1185N/A var fn = this.get("source"),
1185N/A scope = this.get("scope") || this,
1185N/A response;
986N/A
986N/A if(fn) {
986N/A response = fn.call(scope, e.request, this, e);
1185N/A this.fire("data", Y.mix({data:response}, e));
1185N/A }
1185N/A else {
1185N/A e.error = new Error(this.toString() + " Data failure");
1185N/A this.fire("error", e);
1185N/A }
1185N/A
986N/A return e.tId;
1185N/A }
1185N/A});
1185N/A
1185N/AY.DataSource.Function = DSFn;
1185N/A
986N/A