4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly/**
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnelly * Provides a DataSource implementation which can be used to retrieve data from a custom function.
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly *
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @module datasource
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnelly * @submodule datasource-function
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly */
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly/**
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnelly * Function subclass for the DataSource Utility.
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @class DataSource.Function
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @extends DataSource.Local
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @constructor
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly */
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnellyvar LANG = Y.Lang,
8392a541bf432ed8d6e1985b6306b83dc898768dJenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly DSFn = function() {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly DSFn.superclass.constructor.apply(this, arguments);
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly };
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly //
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly // DataSource.Function static properties
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly //
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han DonnellyY.mix(DSFn, {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /**
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * Class name.
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly *
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @property NAME
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @type String
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @static
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @final
1e0aa6df8881a856ec262f103bfee1c0a05d19b9Jenny Han Donnelly * @value "dataSourceFunction"
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly */
1e0aa6df8881a856ec262f103bfee1c0a05d19b9Jenny Han Donnelly NAME: "dataSourceFunction",
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly //
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly // DataSource.Function Attributes
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly //
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /////////////////////////////////////////////////////////////////////////////
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly ATTRS: {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /**
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @attribute source
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @description Pointer to live data.
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @type MIXED
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @default null
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly */
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly source: {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly validator: LANG.isFunction
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly }
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly }
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly});
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han DonnellyY.extend(DSFn, Y.DataSource.Local, {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly /**
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * Passes query string to IO. Fires <code>response</code> event when
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * response is received asynchronously.
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly *
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @method _defRequestFn
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @param e {Event.Facade} Event Facade with the following properties:
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dl>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dt>request (Object)</dt> <dd>The request.</dd>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dl>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dt>success (Function)</dt> <dd>Success handler.</dd>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * </dl>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * </dd>
65b755664437543e907b9557027a4a608cc84c31Jenny Han Donnelly * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * </dl>
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly * @protected
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly */
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly _defRequestFn: function(e) {
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly var fn = this.get("source"),
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload = e.details[0];
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith if (fn) {
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith try {
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.data = fn(e.request, this, e);
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith } catch (ex) {
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith Y.log("Function execution failure", "error", "datasource-function");
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.error = ex;
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly }
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith } else {
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith Y.log("Function data failure", "error", "datasource-function");
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith payload.error = new Error("Function data failure");
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith }
90e5a8d35ce642d0156e8d1d8b8a8ad2bd3417bcLuke Smith
2fe13ddab136a6eb6239d89e5e064e09d9e1bb92Luke Smith this.fire("data", payload);
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly return e.tId;
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly }
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly});
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han Donnelly
4de851bb1d7d556b60e05085a888a1cf06f2b271Jenny Han DonnellyY.DataSource.Function = DSFn;