|
1 YUI.add('datasource-function', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Provides a DataSource implementation which can be used to retrieve data from |
|
5 * a custom function. |
|
6 * |
|
7 * @module datasource |
|
8 * @submodule datasource-function |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Function subclass for the DataSource Utility. |
|
13 * @class DataSource.Function |
|
14 * @extends DataSource.Local |
|
15 * @constructor |
|
16 */ |
|
17 var LANG = Y.Lang, |
|
18 |
|
19 DSFn = function() { |
|
20 DSFn.superclass.constructor.apply(this, arguments); |
|
21 }; |
|
22 |
|
23 |
|
24 ///////////////////////////////////////////////////////////////////////////// |
|
25 // |
|
26 // DataSource.Function static properties |
|
27 // |
|
28 ///////////////////////////////////////////////////////////////////////////// |
|
29 Y.mix(DSFn, { |
|
30 /** |
|
31 * Class name. |
|
32 * |
|
33 * @property NAME |
|
34 * @type String |
|
35 * @static |
|
36 * @final |
|
37 * @value "dataSourceFunction" |
|
38 */ |
|
39 NAME: "dataSourceFunction", |
|
40 |
|
41 |
|
42 ///////////////////////////////////////////////////////////////////////////// |
|
43 // |
|
44 // DataSource.Function Attributes |
|
45 // |
|
46 ///////////////////////////////////////////////////////////////////////////// |
|
47 |
|
48 ATTRS: { |
|
49 /** |
|
50 * Stores the function that will serve the response data. |
|
51 * |
|
52 * @attribute source |
|
53 * @type {Any} |
|
54 * @default null |
|
55 */ |
|
56 source: { |
|
57 validator: LANG.isFunction |
|
58 } |
|
59 } |
|
60 }); |
|
61 |
|
62 Y.extend(DSFn, Y.DataSource.Local, { |
|
63 /** |
|
64 * Passes query data to the source function. Fires <code>response</code> |
|
65 * event with the function results (synchronously). |
|
66 * |
|
67 * @method _defRequestFn |
|
68 * @param e {EventFacade} Event Facade with the following properties: |
|
69 * <dl> |
|
70 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd> |
|
71 * <dt>request (Object)</dt> <dd>The request.</dd> |
|
72 * <dt>callback (Object)</dt> <dd>The callback object with the following |
|
73 * properties: |
|
74 * <dl> |
|
75 * <dt>success (Function)</dt> <dd>Success handler.</dd> |
|
76 * <dt>failure (Function)</dt> <dd>Failure handler.</dd> |
|
77 * </dl> |
|
78 * </dd> |
|
79 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd> |
|
80 * </dl> |
|
81 * @protected |
|
82 */ |
|
83 _defRequestFn: function(e) { |
|
84 var fn = this.get("source"), |
|
85 payload = e.details[0]; |
|
86 |
|
87 if (fn) { |
|
88 try { |
|
89 payload.data = fn(e.request, this, e); |
|
90 } catch (ex) { |
|
91 payload.error = ex; |
|
92 } |
|
93 } else { |
|
94 payload.error = new Error("Function data failure"); |
|
95 } |
|
96 |
|
97 this.fire("data", payload); |
|
98 |
|
99 return e.tId; |
|
100 } |
|
101 }); |
|
102 |
|
103 Y.DataSource.Function = DSFn; |
|
104 |
|
105 |
|
106 }, '@VERSION@', {"requires": ["datasource-local"]}); |